Skip to content

Commit 4ddc75e

Browse files
authored
Improved tasks 30-54
1 parent 0f6735a commit 4ddc75e

File tree

14 files changed

+119
-98
lines changed

14 files changed

+119
-98
lines changed

src/main/ts/g0001_0100/s0030_substring_with_concatenation_of_all_words/solution.ts

+22-22
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,47 @@
22
// #2025_04_01_Time_13_ms_(97.44%)_Space_63.70_MB_(46.03%)
33

44
function findSubstring(s: string, words: string[]): number[] {
5-
let ans: number[] = [];
6-
let n1 = words[0].length;
7-
let n2 = s.length;
8-
let map1 = new Map<string, number>();
5+
let ans: number[] = []
6+
let n1 = words[0].length
7+
let n2 = s.length
8+
let map1 = new Map<string, number>()
99

1010
for (let ch of words) {
11-
map1.set(ch, (map1.get(ch) ?? 0) + 1);
11+
map1.set(ch, (map1.get(ch) ?? 0) + 1)
1212
}
1313

1414
for (let i = 0; i < n1; i++) {
15-
let left = i;
16-
let j = i;
17-
let c = 0;
18-
let map2 = new Map<string, number>();
15+
let left = i
16+
let j = i
17+
let c = 0
18+
let map2 = new Map<string, number>()
1919

2020
while (j + n1 <= n2) {
21-
let word1 = s.substring(j, j + n1);
22-
j += n1;
21+
let word1 = s.substring(j, j + n1)
22+
j += n1
2323

2424
if (map1.has(word1)) {
25-
map2.set(word1, (map2.get(word1) ?? 0) + 1);
26-
c++;
25+
map2.set(word1, (map2.get(word1) ?? 0) + 1)
26+
c++
2727

2828
while ((map2.get(word1) ?? 0) > (map1.get(word1) ?? 0)) {
29-
let word2 = s.substring(left, left + n1);
30-
map2.set(word2, (map2.get(word2) ?? 0) - 1);
31-
left += n1;
32-
c--;
29+
let word2 = s.substring(left, left + n1)
30+
map2.set(word2, (map2.get(word2) ?? 0) - 1)
31+
left += n1
32+
c--
3333
}
3434

3535
if (c === words.length) {
36-
ans.push(left);
36+
ans.push(left)
3737
}
3838
} else {
39-
map2.clear();
40-
c = 0;
41-
left = j;
39+
map2.clear()
40+
c = 0
41+
left = j
4242
}
4343
}
4444
}
45-
return ans;
45+
return ans
4646
}
4747

4848
export { findSubstring }

src/main/ts/g0001_0100/s0036_valid_sudoku/solution.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22
// #Top_Interview_150_Matrix #2025_04_01_Time_1_ms_(99.60%)_Space_58.94_MB_(52.85%)
33

44
function isValidSudoku(board: string[][]): boolean {
5-
let rowSet: number[] = new Array(9).fill(0);
6-
let colSet: number[] = new Array(9).fill(0);
7-
let boxSet: number[] = new Array(9).fill(0);
5+
let rowSet: number[] = new Array(9).fill(0)
6+
let colSet: number[] = new Array(9).fill(0)
7+
let boxSet: number[] = new Array(9).fill(0)
88

99
for (let i = 0; i < 9; i++) {
1010
for (let j = 0; j < 9; j++) {
1111
if (board[i][j] === '.') {
12-
continue;
12+
continue
1313
}
14-
let val = board[i][j].charCodeAt(0) - '0'.charCodeAt(0);
15-
let boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3);
14+
let val = board[i][j].charCodeAt(0) - '0'.charCodeAt(0)
15+
let boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3)
1616

17-
if ((rowSet[i] & (1 << val)) || (colSet[j] & (1 << val)) || (boxSet[boxIndex] & (1 << val))) {
18-
return false;
17+
if (rowSet[i] & (1 << val) || colSet[j] & (1 << val) || boxSet[boxIndex] & (1 << val)) {
18+
return false
1919
}
2020

21-
rowSet[i] |= 1 << val;
22-
colSet[j] |= 1 << val;
23-
boxSet[boxIndex] |= 1 << val;
21+
rowSet[i] |= 1 << val
22+
colSet[j] |= 1 << val
23+
boxSet[boxIndex] |= 1 << val
2424
}
2525
}
26-
return true;
26+
return true
2727
}
2828

2929
export { isValidSudoku }

src/main/ts/g0001_0100/s0050_powx_n/solution.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
// #2025_04_01_Time_0_ms_(100.00%)_Space_57.31_MB_(7.22%)
33

44
function myPow(x: number, n: number): number {
5-
let nn = BigInt(n);
6-
let res = 1.0;
5+
let nn = BigInt(n)
6+
let res = 1.0
77
if (n < 0) {
8-
nn = -nn;
8+
nn = -nn
99
}
1010
while (nn > 0) {
1111
if (nn % 2n === 1n) {
12-
nn--;
13-
res *= x;
12+
nn--
13+
res *= x
1414
} else {
15-
x *= x;
16-
nn /= 2n;
15+
x *= x
16+
nn /= 2n
1717
}
1818
}
19-
return n < 0 ? 1.0 / res : res;
19+
return n < 0 ? 1.0 / res : res
2020
}
2121

2222
export { myPow }

src/main/ts/g0001_0100/s0052_n_queens_ii/solution.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
function totalNQueens(n: number): number {
55
function solve(r: number, cols: boolean[], diag: boolean[], antiDiag: boolean[]): number {
66
if (r === n) {
7-
return 1;
7+
return 1
88
}
9-
let count = 0;
9+
let count = 0
1010
for (let c = 0; c < n; c++) {
1111
if (!cols[c] && !diag[r + c] && !antiDiag[r - c + n - 1]) {
12-
cols[c] = diag[r + c] = antiDiag[r - c + n - 1] = true;
13-
count += solve(r + 1, cols, diag, antiDiag);
14-
cols[c] = diag[r + c] = antiDiag[r - c + n - 1] = false;
12+
cols[c] = diag[r + c] = antiDiag[r - c + n - 1] = true
13+
count += solve(r + 1, cols, diag, antiDiag)
14+
cols[c] = diag[r + c] = antiDiag[r - c + n - 1] = false
1515
}
1616
}
17-
return count;
17+
return count
1818
}
19-
return solve(0, new Array(n).fill(false), new Array(2 * n - 1).fill(false), new Array(2 * n - 1).fill(false));
19+
return solve(0, new Array(n).fill(false), new Array(2 * n - 1).fill(false), new Array(2 * n - 1).fill(false))
2020
}
2121

2222
export { totalNQueens }

src/main/ts/g0001_0100/s0054_spiral_matrix/solution.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,35 @@
33
// #Top_Interview_150_Matrix #2025_04_01_Time_0_ms_(100.00%)_Space_55.71_MB_(30.13%)
44

55
function spiralOrder(matrix: number[][]): number[] {
6-
const result: number[] = [];
7-
let r = 0, c = 0;
8-
let bigR = matrix.length - 1;
9-
let bigC = matrix[0].length - 1;
6+
const result: number[] = []
7+
let r = 0,
8+
c = 0
9+
let bigR = matrix.length - 1
10+
let bigC = matrix[0].length - 1
1011

1112
while (r <= bigR && c <= bigC) {
1213
for (let i = c; i <= bigC; i++) {
13-
result.push(matrix[r][i]);
14+
result.push(matrix[r][i])
1415
}
15-
r++;
16+
r++
1617

1718
for (let i = r; i <= bigR; i++) {
18-
result.push(matrix[i][bigC]);
19+
result.push(matrix[i][bigC])
1920
}
20-
bigC--;
21+
bigC--
2122

2223
for (let i = bigC; i >= c && r <= bigR; i--) {
23-
result.push(matrix[bigR][i]);
24+
result.push(matrix[bigR][i])
2425
}
25-
bigR--;
26+
bigR--
2627

2728
for (let i = bigR; i >= r && c <= bigC; i--) {
28-
result.push(matrix[i][c]);
29+
result.push(matrix[i][c])
2930
}
30-
c++;
31+
c++
3132
}
3233

33-
return result;
34+
return result
3435
}
3536

3637
export { spiralOrder }

src/main/ts/g0101_0200/s0128_longest_consecutive_sequence/solution.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ function longestConsecutive(nums: number[]): number {
77
let maxLen = 0
88
for (let num of sset) {
99
// check its start of the sequence
10-
if (!sset.has(num-1)) {
11-
let len = 0;
12-
while (sset.has(num+len)) {
10+
if (!sset.has(num - 1)) {
11+
let len = 0
12+
while (sset.has(num + len)) {
1313
len += 1
1414
}
1515
maxLen = Math.max(maxLen, len)

src/main/ts/g0201_0300/s0215_kth_largest_element_in_an_array/solution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function findKthLargest(nums: number[], k: number): number {
77
const countingLen = 2e4 + 1
88
const counting = new Int32Array(countingLen)
99
for (const num of nums) {
10-
counting[num + 1e4]++;
10+
counting[num + 1e4]++
1111
}
1212
for (let i = countingLen - 1; i >= 0; i--) {
1313
k -= counting[i]

src/test/ts/g0001_0100/s0014_longest_common_prefix/solution.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { longestCommonPrefix } from 'src/main/ts/g0001_0100/s0014_longest_common
33
import { expect, test } from 'vitest'
44

55
test('longestCommonPrefix', () => {
6-
expect(longestCommonPrefix(['flower','flow','flight'])).toEqual('fl')
6+
expect(longestCommonPrefix(['flower', 'flow', 'flight'])).toEqual('fl')
77
})
88

99
test('longestCommonPrefix2', () => {
10-
expect(longestCommonPrefix(['dog','racecar','car'])).toEqual('')
10+
expect(longestCommonPrefix(['dog', 'racecar', 'car'])).toEqual('')
1111
})
1212

1313
test('longestCommonPrefix3', () => {

src/test/ts/g0001_0100/s0026_remove_duplicates_from_sorted_array/solution.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { removeDuplicates } from 'src/main/ts/g0001_0100/s0026_remove_duplicates
33
import { expect, test } from 'vitest'
44

55
test('removeDuplicates', () => {
6-
expect(removeDuplicates([1,1,2])).toEqual(2)
6+
expect(removeDuplicates([1, 1, 2])).toEqual(2)
77
})
88

99
test('removeDuplicates2', () => {
10-
expect(removeDuplicates([0,0,1,1,1,2,2,3,3,4])).toEqual(5)
10+
expect(removeDuplicates([0, 0, 1, 1, 1, 2, 2, 3, 3, 4])).toEqual(5)
1111
})

src/test/ts/g0001_0100/s0027_remove_element/solution.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { removeElement } from 'src/main/ts/g0001_0100/s0027_remove_element/solut
33
import { expect, test } from 'vitest'
44

55
test('removeElement', () => {
6-
expect(removeElement([3,2,2,3], 3)).toEqual(2)
6+
expect(removeElement([3, 2, 2, 3], 3)).toEqual(2)
77
})
88

99
test('removeElement2', () => {
10-
expect(removeElement([0,1,2,2,3,0,4,2], 2)).toEqual(5)
10+
expect(removeElement([0, 1, 2, 2, 3, 0, 4, 2], 2)).toEqual(5)
1111
})
1212

1313
test('removeElement3', () => {

src/test/ts/g0001_0100/s0030_substring_with_concatenation_of_all_words/solution.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { findSubstring } from 'src/main/ts/g0001_0100/s0030_substring_with_conca
33
import { expect, test } from 'vitest'
44

55
test('findSubstring', () => {
6-
expect(findSubstring('barfoothefoobarman', ['foo','bar'])).toEqual([0,9])
6+
expect(findSubstring('barfoothefoobarman', ['foo', 'bar'])).toEqual([0, 9])
77
})
88

99
test('findSubstring2', () => {
10-
expect(findSubstring('wordgoodgoodgoodbestword', ['word','good','best','word'])).toEqual([])
10+
expect(findSubstring('wordgoodgoodgoodbestword', ['word', 'good', 'best', 'word'])).toEqual([])
1111
})
1212

1313
test('findSubstring3', () => {
14-
expect(findSubstring('barfoofoobarthefoobarman', ['bar','foo','the'])).toEqual([6,9,12])
14+
expect(findSubstring('barfoofoobarthefoobarman', ['bar', 'foo', 'the'])).toEqual([6, 9, 12])
1515
})

src/test/ts/g0001_0100/s0036_valid_sudoku/solution.test.ts

+26-18
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,33 @@ import { isValidSudoku } from 'src/main/ts/g0001_0100/s0036_valid_sudoku/solutio
33
import { expect, test } from 'vitest'
44

55
test('isValidSudoku', () => {
6-
expect(isValidSudoku([['5','3','.','.','7','.','.','.','.']
7-
,['6','.','.','1','9','5','.','.','.']
8-
,['.','9','8','.','.','.','.','6','.']
9-
,['8','.','.','.','6','.','.','.','3']
10-
,['4','.','.','8','.','3','.','.','1']
11-
,['7','.','.','.','2','.','.','.','6']
12-
,['.','6','.','.','.','.','2','8','.']
13-
,['.','.','.','4','1','9','.','.','5']
14-
,['.','.','.','.','8','.','.','7','9']])).toEqual(true)
6+
expect(
7+
isValidSudoku([
8+
['5', '3', '.', '.', '7', '.', '.', '.', '.'],
9+
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
10+
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
11+
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
12+
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
13+
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
14+
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
15+
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
16+
['.', '.', '.', '.', '8', '.', '.', '7', '9'],
17+
]),
18+
).toEqual(true)
1519
})
1620

1721
test('isValidSudoku2', () => {
18-
expect(isValidSudoku([['8','3','.','.','7','.','.','.','.']
19-
,['6','.','.','1','9','5','.','.','.']
20-
,['.','9','8','.','.','.','.','6','.']
21-
,['8','.','.','.','6','.','.','.','3']
22-
,['4','.','.','8','.','3','.','.','1']
23-
,['7','.','.','.','2','.','.','.','6']
24-
,['.','6','.','.','.','.','2','8','.']
25-
,['.','.','.','4','1','9','.','.','5']
26-
,['.','.','.','.','8','.','.','7','9']])).toEqual(false)
22+
expect(
23+
isValidSudoku([
24+
['8', '3', '.', '.', '7', '.', '.', '.', '.'],
25+
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
26+
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
27+
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
28+
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
29+
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
30+
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
31+
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
32+
['.', '.', '.', '.', '8', '.', '.', '7', '9'],
33+
]),
34+
).toEqual(false)
2735
})

src/test/ts/g0001_0100/s0050_powx_n/solution.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { myPow } from 'src/main/ts/g0001_0100/s0050_powx_n/solution'
33
import { expect, test } from 'vitest'
44

55
test('myPow', () => {
6-
expect(myPow(2.00000, 10)).toEqual(1024.00000)
6+
expect(myPow(2.0, 10)).toEqual(1024.0)
77
})
88

99
test('myPow2', () => {
10-
expect(myPow(2.10000, 3)).toEqual(9.261000000000001)
10+
expect(myPow(2.1, 3)).toEqual(9.261000000000001)
1111
})
1212

1313
test('myPow3', () => {
14-
expect(myPow(2.00000, -2)).toEqual(0.25000)
14+
expect(myPow(2.0, -2)).toEqual(0.25)
1515
})

src/test/ts/g0001_0100/s0054_spiral_matrix/solution.test.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@ import { spiralOrder } from 'src/main/ts/g0001_0100/s0054_spiral_matrix/solution
33
import { expect, test } from 'vitest'
44

55
test('spiralOrder', () => {
6-
expect(spiralOrder([[1,2,3],[4,5,6],[7,8,9]])).toEqual([1,2,3,6,9,8,7,4,5])
6+
expect(
7+
spiralOrder([
8+
[1, 2, 3],
9+
[4, 5, 6],
10+
[7, 8, 9],
11+
]),
12+
).toEqual([1, 2, 3, 6, 9, 8, 7, 4, 5])
713
})
814

915
test('spiralOrder2', () => {
10-
expect(spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12]])).toEqual([1,2,3,4,8,12,11,10,9,5,6,7])
16+
expect(
17+
spiralOrder([
18+
[1, 2, 3, 4],
19+
[5, 6, 7, 8],
20+
[9, 10, 11, 12],
21+
]),
22+
).toEqual([1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7])
1123
})

0 commit comments

Comments
 (0)