Skip to content

Commit abc3e3f

Browse files
committedApr 2, 2025·
Added tasks 12-54
1 parent 8833331 commit abc3e3f

File tree

14 files changed

+1016
-134
lines changed

14 files changed

+1016
-134
lines changed
 

‎README.md

Lines changed: 161 additions & 130 deletions
Large diffs are not rendered by default.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 12\. Integer to Roman
5+
6+
Medium
7+
8+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
9+
10+
Symbol Value
11+
I 1
12+
V 5
13+
X 10
14+
L 50
15+
C 100
16+
D 500
17+
M 1000
18+
19+
For example, `2` is written as `II` in Roman numeral, just two one's added together. `12` is written as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.
20+
21+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
22+
23+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
24+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
25+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
26+
27+
Given an integer, convert it to a roman numeral.
28+
29+
**Example 1:**
30+
31+
**Input:** num = 3
32+
33+
**Output:** "III"
34+
35+
**Example 2:**
36+
37+
**Input:** num = 4
38+
39+
**Output:** "IV"
40+
41+
**Example 3:**
42+
43+
**Input:** num = 9
44+
45+
**Output:** "IX"
46+
47+
**Example 4:**
48+
49+
**Input:** num = 58
50+
51+
**Output:** "LVIII"
52+
53+
**Explanation:** L = 50, V = 5, III = 3.
54+
55+
**Example 5:**
56+
57+
**Input:** num = 1994
58+
59+
**Output:** "MCMXCIV"
60+
61+
**Explanation:** M = 1000, CM = 900, XC = 90 and IV = 4.
62+
63+
**Constraints:**
64+
65+
* `1 <= num <= 3999`
66+
67+
## Solution
68+
69+
```typescript
70+
function intToRoman(num: number): string {
71+
const values = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
72+
const symbols = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']
73+
let result = ''
74+
let i = 0
75+
while (num > 0) {
76+
if (num >= values[i]) {
77+
result += symbols[i]
78+
num -= values[i]
79+
} else {
80+
i++
81+
}
82+
}
83+
return result
84+
}
85+
86+
export { intToRoman }
87+
```
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 13\. Roman to Integer
5+
6+
Easy
7+
8+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
9+
10+
Symbol Value
11+
I 1
12+
V 5
13+
X 10
14+
L 50
15+
C 100
16+
D 500
17+
M 1000
18+
19+
For example, `2` is written as `II` in Roman numeral, just two one's added together. `12` is written as `XII`, which is simply `X + II`. The number `27` is written as `XXVII`, which is `XX + V + II`.
20+
21+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
22+
23+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9.
24+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90.
25+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
26+
27+
Given a roman numeral, convert it to an integer.
28+
29+
**Example 1:**
30+
31+
**Input:** s = "III"
32+
33+
**Output:** 3
34+
35+
**Example 2:**
36+
37+
**Input:** s = "IV"
38+
39+
**Output:** 4
40+
41+
**Example 3:**
42+
43+
**Input:** s = "IX"
44+
45+
**Output:** 9
46+
47+
**Example 4:**
48+
49+
**Input:** s = "LVIII"
50+
51+
**Output:** 58
52+
53+
**Explanation:** L = 50, V= 5, III = 3.
54+
55+
**Example 5:**
56+
57+
**Input:** s = "MCMXCIV"
58+
59+
**Output:** 1994
60+
61+
**Explanation:** M = 1000, CM = 900, XC = 90 and IV = 4.
62+
63+
**Constraints:**
64+
65+
* `1 <= s.length <= 15`
66+
* `s` contains only the characters `('I', 'V', 'X', 'L', 'C', 'D', 'M')`.
67+
* It is **guaranteed** that `s` is a valid roman numeral in the range `[1, 3999]`.
68+
69+
## Solution
70+
71+
```typescript
72+
function romanToInt(s: string): number {
73+
let x = 0
74+
let y: string
75+
for (let i = 0; i < s.length; i++) {
76+
y = s.charAt(i)
77+
switch (y) {
78+
case 'I':
79+
x = getX(s, x, i, 1, 'V', 'X')
80+
break
81+
case 'V':
82+
x += 5
83+
break
84+
case 'X':
85+
x = getX(s, x, i, 10, 'L', 'C')
86+
break
87+
case 'L':
88+
x += 50
89+
break
90+
case 'C':
91+
x = getX(s, x, i, 100, 'D', 'M')
92+
break
93+
case 'D':
94+
x += 500
95+
break
96+
case 'M':
97+
x += 1000
98+
break
99+
default:
100+
break
101+
}
102+
}
103+
return x
104+
}
105+
106+
function getX(s: string, x: number, i: number, i2: number, v: string, x2: string): number {
107+
if (i + 1 === s.length) {
108+
x += i2
109+
} else if (s.charAt(i + 1) === v || s.charAt(i + 1) === x2) {
110+
x -= i2
111+
} else {
112+
x += i2
113+
}
114+
return x
115+
}
116+
117+
export { romanToInt }
118+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 14\. Longest Common Prefix
5+
6+
Easy
7+
8+
Write a function to find the longest common prefix string amongst an array of strings.
9+
10+
If there is no common prefix, return an empty string `""`.
11+
12+
**Example 1:**
13+
14+
**Input:** strs = ["flower","flow","flight"]
15+
16+
**Output:** "fl"
17+
18+
**Example 2:**
19+
20+
**Input:** strs = ["dog","racecar","car"]
21+
22+
**Output:** ""
23+
24+
**Explanation:** There is no common prefix among the input strings.
25+
26+
**Constraints:**
27+
28+
* `1 <= strs.length <= 200`
29+
* `0 <= strs[i].length <= 200`
30+
* `strs[i]` consists of only lower-case English letters.
31+
32+
## Solution
33+
34+
```typescript
35+
function longestCommonPrefix(strs: string[]): string {
36+
if (strs.length < 1) {
37+
return ''
38+
}
39+
if (strs.length === 1) {
40+
return strs[0]
41+
}
42+
let temp = strs[0]
43+
let i = 1
44+
let cur: string
45+
while (temp.length > 0 && i < strs.length) {
46+
if (temp.length > strs[i].length) {
47+
temp = temp.substring(0, strs[i].length)
48+
}
49+
cur = strs[i].substring(0, temp.length)
50+
if (cur !== temp) {
51+
temp = temp.substring(0, temp.length - 1)
52+
} else {
53+
i++
54+
}
55+
}
56+
return temp
57+
}
58+
59+
export { longestCommonPrefix }
60+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 26\. Remove Duplicates from Sorted Array
5+
6+
Easy
7+
8+
Given an integer array `nums` sorted in **non-decreasing order**, remove the duplicates [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) such that each unique element appears only **once**. The **relative order** of the elements should be kept the **same**.
9+
10+
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
11+
12+
Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.
13+
14+
Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory.
15+
16+
**Custom Judge:**
17+
18+
The judge will test your solution with the following code:
19+
20+
int[] nums = [...]; // Input array
21+
int[] expectedNums = [...]; // The expected answer with correct length
22+
23+
int k = removeDuplicates(nums); // Calls your implementation
24+
25+
assert k == expectedNums.length;
26+
for (int i = 0; i < k; i++) {
27+
assert nums[i] == expectedNums[i];
28+
}
29+
30+
If all assertions pass, then your solution will be **accepted**.
31+
32+
**Example 1:**
33+
34+
**Input:** nums = [1,1,2]
35+
36+
**Output:** 2, nums = [1,2,\_]
37+
38+
**Explanation:** Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
39+
40+
**Example 2:**
41+
42+
**Input:** nums = [0,0,1,1,1,2,2,3,3,4]
43+
44+
**Output:** 5, nums = [0,1,2,3,4,\_,\_,\_,\_,\_]
45+
46+
**Explanation:** Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
47+
48+
**Constraints:**
49+
50+
* <code>0 <= nums.length <= 3 * 10<sup>4</sup></code>
51+
* `-100 <= nums[i] <= 100`
52+
* `nums` is sorted in **non-decreasing** order.
53+
54+
## Solution
55+
56+
```typescript
57+
function removeDuplicates(nums: number[]): number {
58+
let n = nums.length
59+
let i = 0
60+
let j = 1
61+
if (n <= 1) {
62+
return n
63+
}
64+
while (j <= n - 1) {
65+
if (nums[i] !== nums[j]) {
66+
nums[i + 1] = nums[j]
67+
i++
68+
}
69+
j++
70+
}
71+
return i + 1
72+
}
73+
74+
export { removeDuplicates }
75+
```
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 27\. Remove Element
5+
6+
Easy
7+
8+
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm). The relative order of the elements may be changed.
9+
10+
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
11+
12+
Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.
13+
14+
Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory.
15+
16+
**Custom Judge:**
17+
18+
The judge will test your solution with the following code:
19+
20+
int[] nums = [...]; // Input array
21+
int val = ...; // Value to remove
22+
int[] expectedNums = [...]; // The expected answer with correct length.
23+
// It is sorted with no values equaling val.
24+
25+
int k = removeElement(nums, val); // Calls your implementation
26+
27+
assert k == expectedNums.length;
28+
sort(nums, 0, k); // Sort the first k elements of nums
29+
for (int i = 0; i < actualLength; i++) {
30+
assert nums[i] == expectedNums[i];
31+
}
32+
33+
If all assertions pass, then your solution will be **accepted**.
34+
35+
**Example 1:**
36+
37+
**Input:** nums = [3,2,2,3], val = 3
38+
39+
**Output:** 2, nums = [2,2,\_,\_]
40+
41+
**Explanation:** Your function should return k = 2, with the first two elements of nums being 2. It does not matter what you leave beyond the returned k (hence they are underscores).
42+
43+
**Example 2:**
44+
45+
**Input:** nums = [0,1,2,2,3,0,4,2], val = 2
46+
47+
**Output:** 5, nums = [0,1,4,0,3,\_,\_,\_]
48+
49+
**Explanation:** Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4. Note that the five elements can be returned in any order. It does not matter what you leave beyond the returned k (hence they are underscores).
50+
51+
**Constraints:**
52+
53+
* `0 <= nums.length <= 100`
54+
* `0 <= nums[i] <= 50`
55+
* `0 <= val <= 100`
56+
57+
## Solution
58+
59+
```typescript
60+
function removeElement(nums: number[], val: number): number {
61+
if (!nums || nums.length === 0) {
62+
return 0
63+
}
64+
let len = nums.length
65+
let j = len - 1
66+
let occurTimes = 0
67+
for (let i = 0; i < len; i++) {
68+
if (nums[i] === val) {
69+
occurTimes++
70+
if (j === i) {
71+
return len - occurTimes
72+
}
73+
while (nums[j] === val) {
74+
j--
75+
occurTimes++
76+
if (j === i) {
77+
return len - occurTimes
78+
}
79+
}
80+
nums[i] = nums[j]
81+
j--
82+
}
83+
if (i === j) {
84+
return len - occurTimes
85+
}
86+
}
87+
return len - occurTimes
88+
}
89+
90+
export { removeElement }
91+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 28\. Implement strStr()
5+
6+
Easy
7+
8+
Implement [strStr()](http://www.cplusplus.com/reference/cstring/strstr/).
9+
10+
Return the index of the first occurrence of needle in haystack, or `-1` if `needle` is not part of `haystack`.
11+
12+
**Clarification:**
13+
14+
What should we return when `needle` is an empty string? This is a great question to ask during an interview.
15+
16+
For the purpose of this problem, we will return 0 when `needle` is an empty string. This is consistent to C's [strstr()](http://www.cplusplus.com/reference/cstring/strstr/) and Java's [indexOf()](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)).
17+
18+
**Example 1:**
19+
20+
**Input:** haystack = "hello", needle = "ll"
21+
22+
**Output:** 2
23+
24+
**Example 2:**
25+
26+
**Input:** haystack = "aaaaa", needle = "bba"
27+
28+
**Output:** -1
29+
30+
**Example 3:**
31+
32+
**Input:** haystack = "", needle = ""
33+
34+
**Output:** 0
35+
36+
**Constraints:**
37+
38+
* <code>0 <= haystack.length, needle.length <= 5 * 10<sup>4</sup></code>
39+
* `haystack` and `needle` consist of only lower-case English characters.
40+
41+
## Solution
42+
43+
```typescript
44+
function strStr(haystack: string, needle: string): number {
45+
if (needle.length === 0) {
46+
return 0
47+
}
48+
let m = haystack.length
49+
let n = needle.length
50+
for (let start = 0; start <= m - n; start++) {
51+
if (haystack.substring(start, start + n) === needle) {
52+
return start
53+
}
54+
}
55+
return -1
56+
}
57+
58+
export { strStr }
59+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 30\. Substring with Concatenation of All Words
5+
6+
Hard
7+
8+
You are given a string `s` and an array of strings `words` of **the same length**. Return all starting indices of substring(s) in `s` that is a concatenation of each word in `words` **exactly once**, **in any order**, and **without any intervening characters**.
9+
10+
You can return the answer in **any order**.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "barfoothefoobarman", words = ["foo","bar"]
15+
16+
**Output:** [0,9]
17+
18+
**Explanation:** Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively. The output order does not matter, returning [9,0] is fine too.
19+
20+
**Example 2:**
21+
22+
**Input:** s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
23+
24+
**Output:** []
25+
26+
**Example 3:**
27+
28+
**Input:** s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
29+
30+
**Output:** [6,9,12]
31+
32+
**Constraints:**
33+
34+
* <code>1 <= s.length <= 10<sup>4</sup></code>
35+
* `s` consists of lower-case English letters.
36+
* `1 <= words.length <= 5000`
37+
* `1 <= words[i].length <= 30`
38+
* `words[i]` consists of lower-case English letters.
39+
40+
## Solution
41+
42+
```typescript
43+
function findSubstring(s: string, words: string[]): number[] {
44+
let ans: number[] = []
45+
let n1 = words[0].length
46+
let n2 = s.length
47+
let map1 = new Map<string, number>()
48+
49+
for (let ch of words) {
50+
map1.set(ch, (map1.get(ch) ?? 0) + 1)
51+
}
52+
53+
for (let i = 0; i < n1; i++) {
54+
let left = i
55+
let j = i
56+
let c = 0
57+
let map2 = new Map<string, number>()
58+
59+
while (j + n1 <= n2) {
60+
let word1 = s.substring(j, j + n1)
61+
j += n1
62+
63+
if (map1.has(word1)) {
64+
map2.set(word1, (map2.get(word1) ?? 0) + 1)
65+
c++
66+
67+
while ((map2.get(word1) ?? 0) > (map1.get(word1) ?? 0)) {
68+
let word2 = s.substring(left, left + n1)
69+
map2.set(word2, (map2.get(word2) ?? 0) - 1)
70+
left += n1
71+
c--
72+
}
73+
74+
if (c === words.length) {
75+
ans.push(left)
76+
}
77+
} else {
78+
map2.clear()
79+
c = 0
80+
left = j
81+
}
82+
}
83+
}
84+
return ans
85+
}
86+
87+
export { findSubstring }
88+
```
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 36\. Valid Sudoku
5+
6+
Medium
7+
8+
Determine if a `9 x 9` Sudoku board is valid. Only the filled cells need to be validated **according to the following rules**:
9+
10+
1. Each row must contain the digits `1-9` without repetition.
11+
2. Each column must contain the digits `1-9` without repetition.
12+
3. Each of the nine `3 x 3` sub-boxes of the grid must contain the digits `1-9` without repetition.
13+
14+
**Note:**
15+
16+
* A Sudoku board (partially filled) could be valid but is not necessarily solvable.
17+
* Only the filled cells need to be validated according to the mentioned rules.
18+
19+
**Example 1:**
20+
21+
![](https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png)
22+
23+
**Input:**
24+
25+
board =
26+
[["5","3",".",".","7",".",".",".","."]
27+
,["6",".",".","1","9","5",".",".","."]
28+
,[".","9","8",".",".",".",".","6","."]
29+
,["8",".",".",".","6",".",".",".","3"]
30+
,["4",".",".","8",".","3",".",".","1"]
31+
,["7",".",".",".","2",".",".",".","6"]
32+
,[".","6",".",".",".",".","2","8","."]
33+
,[".",".",".","4","1","9",".",".","5"]
34+
,[".",".",".",".","8",".",".","7","9"]]
35+
36+
**Output:** true
37+
38+
**Example 2:**
39+
40+
**Input:**
41+
42+
board =
43+
[["8","3",".",".","7",".",".",".","."]
44+
,["6",".",".","1","9","5",".",".","."]
45+
,[".","9","8",".",".",".",".","6","."]
46+
,["8",".",".",".","6",".",".",".","3"]
47+
,["4",".",".","8",".","3",".",".","1"]
48+
,["7",".",".",".","2",".",".",".","6"]
49+
,[".","6",".",".",".",".","2","8","."]
50+
,[".",".",".","4","1","9",".",".","5"]
51+
,[".",".",".",".","8",".",".","7","9"]]
52+
53+
**Output:** false
54+
55+
**Explanation:** Same as Example 1, except with the **5** in the top left corner being modified to **8**. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
56+
57+
**Constraints:**
58+
59+
* `board.length == 9`
60+
* `board[i].length == 9`
61+
* `board[i][j]` is a digit `1-9` or `'.'`.
62+
63+
## Solution
64+
65+
```typescript
66+
function isValidSudoku(board: string[][]): boolean {
67+
let rowSet: number[] = new Array(9).fill(0)
68+
let colSet: number[] = new Array(9).fill(0)
69+
let boxSet: number[] = new Array(9).fill(0)
70+
71+
for (let i = 0; i < 9; i++) {
72+
for (let j = 0; j < 9; j++) {
73+
if (board[i][j] === '.') {
74+
continue
75+
}
76+
let val = board[i][j].charCodeAt(0) - '0'.charCodeAt(0)
77+
let boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3)
78+
79+
if (rowSet[i] & (1 << val) || colSet[j] & (1 << val) || boxSet[boxIndex] & (1 << val)) {
80+
return false
81+
}
82+
83+
rowSet[i] |= 1 << val
84+
colSet[j] |= 1 << val
85+
boxSet[boxIndex] |= 1 << val
86+
}
87+
}
88+
return true
89+
}
90+
91+
export { isValidSudoku }
92+
```
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 50\. Pow(x, n)
5+
6+
Medium
7+
8+
Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which calculates `x` raised to the power `n` (i.e., <code>x<sup>n</sup></code>).
9+
10+
**Example 1:**
11+
12+
**Input:** x = 2.00000, n = 10
13+
14+
**Output:** 1024.00000
15+
16+
**Example 2:**
17+
18+
**Input:** x = 2.10000, n = 3
19+
20+
**Output:** 9.26100
21+
22+
**Example 3:**
23+
24+
**Input:** x = 2.00000, n = -2
25+
26+
**Output:** 0.25000
27+
28+
**Explanation:** 2<sup>\-2</sup> = 1/2<sup>2</sup> = 1/4 = 0.25
29+
30+
**Constraints:**
31+
32+
* `-100.0 < x < 100.0`
33+
* <code>-2<sup>31</sup> <= n <= 2<sup>31</sup>-1</code>
34+
* <code>-10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup></code>
35+
36+
## Solution
37+
38+
```typescript
39+
function myPow(x: number, n: number): number {
40+
let nn = BigInt(n)
41+
let res = 1.0
42+
if (n < 0) {
43+
nn = -nn
44+
}
45+
while (nn > 0) {
46+
if (nn % 2n === 1n) {
47+
nn--
48+
res *= x
49+
} else {
50+
x *= x
51+
nn /= 2n
52+
}
53+
}
54+
return n < 0 ? 1.0 / res : res
55+
}
56+
57+
export { myPow }
58+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 52\. N-Queens II
5+
6+
Hard
7+
8+
The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
9+
10+
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
15+
16+
**Input:** n = 4
17+
18+
**Output:** 2
19+
20+
**Explanation:** There are two distinct solutions to the 4-queens puzzle as shown.
21+
22+
**Example 2:**
23+
24+
**Input:** n = 1
25+
26+
**Output:** 1
27+
28+
**Constraints:**
29+
30+
* `1 <= n <= 9`
31+
32+
## Solution
33+
34+
```typescript
35+
function totalNQueens(n: number): number {
36+
function solve(r: number, cols: boolean[], diag: boolean[], antiDiag: boolean[]): number {
37+
if (r === n) {
38+
return 1
39+
}
40+
let count = 0
41+
for (let c = 0; c < n; c++) {
42+
if (!cols[c] && !diag[r + c] && !antiDiag[r - c + n - 1]) {
43+
cols[c] = diag[r + c] = antiDiag[r - c + n - 1] = true
44+
count += solve(r + 1, cols, diag, antiDiag)
45+
cols[c] = diag[r + c] = antiDiag[r - c + n - 1] = false
46+
}
47+
}
48+
return count
49+
}
50+
return solve(0, new Array(n).fill(false), new Array(2 * n - 1).fill(false), new Array(2 * n - 1).fill(false))
51+
}
52+
53+
export { totalNQueens }
54+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 54\. Spiral Matrix
5+
6+
Medium
7+
8+
Given an `m x n` `matrix`, return _all elements of the_ `matrix` _in spiral order_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg)
13+
14+
**Input:** matrix = \[\[1,2,3],[4,5,6],[7,8,9]]
15+
16+
**Output:** [1,2,3,6,9,8,7,4,5]
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/11/13/spiral.jpg)
21+
22+
**Input:** matrix = \[\[1,2,3,4],[5,6,7,8],[9,10,11,12]]
23+
24+
**Output:** [1,2,3,4,8,12,11,10,9,5,6,7]
25+
26+
**Constraints:**
27+
28+
* `m == matrix.length`
29+
* `n == matrix[i].length`
30+
* `1 <= m, n <= 10`
31+
* `-100 <= matrix[i][j] <= 100`
32+
33+
## Solution
34+
35+
```typescript
36+
function spiralOrder(matrix: number[][]): number[] {
37+
const result: number[] = []
38+
let r = 0,
39+
c = 0
40+
let bigR = matrix.length - 1
41+
let bigC = matrix[0].length - 1
42+
43+
while (r <= bigR && c <= bigC) {
44+
for (let i = c; i <= bigC; i++) {
45+
result.push(matrix[r][i])
46+
}
47+
r++
48+
49+
for (let i = r; i <= bigR; i++) {
50+
result.push(matrix[i][bigC])
51+
}
52+
bigC--
53+
54+
for (let i = bigC; i >= c && r <= bigR; i--) {
55+
result.push(matrix[bigR][i])
56+
}
57+
bigR--
58+
59+
for (let i = bigR; i >= r && c <= bigC; i--) {
60+
result.push(matrix[i][c])
61+
}
62+
c++
63+
}
64+
65+
return result
66+
}
67+
68+
export { spiralOrder }
69+
```

‎src/main/ts/g0101_0200/s0128_longest_consecutive_sequence/readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ function longestConsecutive(nums: number[]): number {
3636
let maxLen = 0
3737
for (let num of sset) {
3838
// check its start of the sequence
39-
if (!sset.has(num-1)) {
40-
let len = 0;
41-
while (sset.has(num+len)) {
39+
if (!sset.has(num - 1)) {
40+
let len = 0
41+
while (sset.has(num + len)) {
4242
len += 1
4343
}
4444
maxLen = Math.max(maxLen, len)

‎src/main/ts/g0201_0300/s0215_kth_largest_element_in_an_array/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function findKthLargest(nums: number[], k: number): number {
3333
const countingLen = 2e4 + 1
3434
const counting = new Int32Array(countingLen)
3535
for (const num of nums) {
36-
counting[num + 1e4]++;
36+
counting[num + 1e4]++
3737
}
3838
for (let i = countingLen - 1; i >= 0; i--) {
3939
k -= counting[i]

0 commit comments

Comments
 (0)
Please sign in to comment.