Skip to content

Commit 35c2690

Browse files
committed
feat: add new solution and format code
1 parent db30b17 commit 35c2690

File tree

51 files changed

+470
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+470
-229
lines changed

lcof2/剑指 Offer II 034. 外星语言是否排序/Solution.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ function isAlienSorted(words: string[], order: string): boolean {
66
function compare(str1: string, str2: string): boolean {
77
const n = Math.min(str1.length, str2.length);
88
for (let i = 0; i < n; i++) {
9-
let k1 = str1[i], k2 = str2[i];
9+
let k1 = str1[i],
10+
k2 = str2[i];
1011
if (k1 != k2) return charMap.get(k1) < charMap.get(k2);
1112
}
1213
return n == str1.length;
@@ -15,4 +16,4 @@ function isAlienSorted(words: string[], order: string): boolean {
1516
if (!compare(words[i - 1], words[i])) return false;
1617
}
1718
return true;
18-
};
19+
}

lcof2/剑指 Offer II 042. 最近请求次数/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class RecentCounter {
99
ping(t: number): number {
1010
while (this.stack.length && this.stack[0] + 3000 < t) {
1111
this.cnt--;
12-
this.stack.shift()
12+
this.stack.shift();
1313
}
1414
this.cnt++;
1515
this.stack.push(t);
1616
return this.cnt;
1717
}
18-
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
function search(nums: number[], target: number): number {
2-
const n = nums.length;
3-
let left = 0,
4-
right = n - 1;
5-
while (left < right) {
6-
const mid = (left + right) >> 1;
7-
if (nums[0] <= nums[mid]) {
8-
if (nums[0] <= target && target <= nums[mid]) {
9-
right = mid;
10-
} else {
11-
left = mid + 1;
12-
}
13-
} else {
14-
if (nums[mid] < target && target <= nums[n - 1]) {
15-
left = mid + 1;
16-
} else {
17-
right = mid;
18-
}
19-
}
20-
}
21-
return nums[left] == target ? left : -1;
22-
}
1+
function search(nums: number[], target: number): number {
2+
const n = nums.length;
3+
let left = 0,
4+
right = n - 1;
5+
while (left < right) {
6+
const mid = (left + right) >> 1;
7+
if (nums[0] <= nums[mid]) {
8+
if (nums[0] <= target && target <= nums[mid]) {
9+
right = mid;
10+
} else {
11+
left = mid + 1;
12+
}
13+
} else {
14+
if (nums[mid] < target && target <= nums[n - 1]) {
15+
left = mid + 1;
16+
} else {
17+
right = mid;
18+
}
19+
}
20+
}
21+
return nums[left] == target ? left : -1;
22+
}

solution/0000-0099/0064.Minimum Path Sum/Solution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ function minPathSum(grid: number[][]): number {
1515
}
1616
}
1717
return dp[m - 1][n - 1];
18-
};
18+
}

solution/0100-0199/0118.Pascal's Triangle/Solution.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ class Solution:
22
def generate(self, numRows: int) -> List[List[int]]:
33
ans = []
44
for i in range(numRows):
5-
t = [1 if j == 0 or j == i else ans[-1][j] + ans[-1][j - 1]
6-
for j in range(i + 1)]
5+
t = [
6+
1 if j == 0 or j == i else ans[-1][j] + ans[-1][j - 1]
7+
for j in range(i + 1)
8+
]
79
ans.append(t)
810
return ans

solution/0100-0199/0130.Surrounded Regions/Solution.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
Do not return anything, modify board in-place instead.
33
*/
4-
function solve(board: string[][]): void {
4+
function solve(board: string[][]): void {
55
function dfs(i, j) {
66
board[i][j] = '.';
77
const dirs = [-1, 0, 1, 0, -1];
@@ -17,7 +17,10 @@
1717
const n = board[0].length;
1818
for (let i = 0; i < m; ++i) {
1919
for (let j = 0; j < n; ++j) {
20-
if ((i == 0 || i == m - 1 || j == 0 || j == n - 1) && board[i][j] == 'O') {
20+
if (
21+
(i == 0 || i == m - 1 || j == 0 || j == n - 1) &&
22+
board[i][j] == 'O'
23+
) {
2124
dfs(i, j);
2225
}
2326
}
@@ -31,4 +34,4 @@
3134
}
3235
}
3336
}
34-
};
37+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
function findMin(nums: number[]): number {
2-
const n = nums.length;
3-
if (nums[0] <= nums[n - 1]) {
4-
return nums[0];
5-
}
6-
let left = 0,
7-
right = n - 1;
8-
while (left < right) {
9-
const mid = (left + right) >> 1;
10-
if (nums[0] <= nums[mid]) {
11-
left = mid + 1;
12-
} else {
13-
right = mid;
14-
}
15-
}
16-
return nums[left];
17-
}
1+
function findMin(nums: number[]): number {
2+
const n = nums.length;
3+
if (nums[0] <= nums[n - 1]) {
4+
return nums[0];
5+
}
6+
let left = 0,
7+
right = n - 1;
8+
while (left < right) {
9+
const mid = (left + right) >> 1;
10+
if (nums[0] <= nums[mid]) {
11+
left = mid + 1;
12+
} else {
13+
right = mid;
14+
}
15+
}
16+
return nums[left];
17+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
function findMin(nums: number[]): number {
2-
let left = 0,
3-
right = nums.length - 1;
4-
while (left < right) {
5-
const mid = (left + right) >> 1;
6-
if (nums[mid] > nums[right]) {
7-
left = mid + 1;
8-
} else if (nums[mid] < nums[right]) {
9-
right = mid;
10-
} else {
11-
--right;
12-
}
13-
}
14-
return nums[left];
15-
}
1+
function findMin(nums: number[]): number {
2+
let left = 0,
3+
right = nums.length - 1;
4+
while (left < right) {
5+
const mid = (left + right) >> 1;
6+
if (nums[mid] > nums[right]) {
7+
left = mid + 1;
8+
} else if (nums[mid] < nums[right]) {
9+
right = mid;
10+
} else {
11+
--right;
12+
}
13+
}
14+
return nums[left];
15+
}

solution/0200-0299/0208.Implement Trie (Prefix Tree)/Solution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ class Trie {
4343
}
4444
return head;
4545
}
46-
}
46+
}
+14-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
function hIndex(citations: number[]): number {
2-
const n = citations.length;
3-
let left = 0,
4-
right = n;
5-
while (left < right) {
6-
const mid = (left + right + 1) >> 1;
7-
if (citations[n - mid] >= mid) {
8-
left = mid;
9-
} else {
10-
right = mid - 1;
11-
}
12-
}
13-
return left;
14-
}
1+
function hIndex(citations: number[]): number {
2+
const n = citations.length;
3+
let left = 0,
4+
right = n;
5+
while (left < right) {
6+
const mid = (left + right + 1) >> 1;
7+
if (citations[n - mid] >= mid) {
8+
left = mid;
9+
} else {
10+
right = mid - 1;
11+
}
12+
}
13+
return left;
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
function findDuplicate(nums: number[]): number {
2-
let left = 1,
3-
right = nums.length - 1;
4-
while (left < right) {
5-
const mid = (left + right) >> 1;
6-
let cnt = 0;
7-
for (let v of nums) {
8-
if (v <= mid) {
9-
++cnt;
10-
}
11-
}
12-
if (cnt > mid) {
13-
right = mid;
14-
} else {
15-
left = mid + 1;
16-
}
17-
}
18-
return left;
19-
}
1+
function findDuplicate(nums: number[]): number {
2+
let left = 1,
3+
right = nums.length - 1;
4+
while (left < right) {
5+
const mid = (left + right) >> 1;
6+
let cnt = 0;
7+
for (let v of nums) {
8+
if (v <= mid) {
9+
++cnt;
10+
}
11+
}
12+
if (cnt > mid) {
13+
right = mid;
14+
} else {
15+
left = mid + 1;
16+
}
17+
}
18+
return left;
19+
}

solution/0300-0399/0300.Longest Increasing Subsequence/Solution.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ function lengthOfLIS(nums: number[]): number {
77
if (nums[i] > d[size]) {
88
d[++size] = nums[i];
99
} else {
10-
let left = 1, right = size;
10+
let left = 1,
11+
right = size;
1112
while (left < right) {
1213
const mid = (left + right) >> 1;
1314
if (d[mid] >= nums[i]) {
@@ -21,4 +22,4 @@ function lengthOfLIS(nums: number[]): number {
2122
}
2223
}
2324
return size;
24-
};
25+
}

solution/0300-0399/0309.Best Time to Buy and Sell Stock with Cooldown/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ function maxProfit(prices: number[]): number {
66
dp[i] = [
77
Math.max(dp[i - 1][0], dp[i - 1][2]),
88
Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]),
9-
dp[i - 1][1] + prices[i]
9+
dp[i - 1][1] + prices[i],
1010
];
1111
}
1212
return Math.max(dp[n - 1][0], dp[n - 1][2]);
13-
};
13+
}

solution/0300-0399/0357.Count Numbers with Unique Digits/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ def countNumbersWithUniqueDigits(self, n: int) -> int:
66
return 10
77
ans, cur = 10, 9
88
for i in range(n - 1):
9-
cur *= (9 - i)
9+
cur *= 9 - i
1010
ans += cur
1111
return ans

solution/0300-0399/0368.Largest Divisible Subset/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
2020
while len(ans) < max_len:
2121
ans.append(nums[max_index])
2222
max_index = p[max_index]
23-
return ans[::-1]
23+
return ans[::-1]

solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class RandomizedSet:
2-
32
def __init__(self):
43
self.m = {}
54
self.l = []
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
function findLUSlength(a: string, b: string): number {
22
return a != b ? Math.max(a.length, b.length) : -1;
3-
};
3+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
function complexNumberMultiply(num1: string, num2: string): string {
2-
let arr1 = num1.split('+'), arr2 = num2.split('+');;
3-
let r1 = Number(arr1[0]), r2 = Number(arr2[0]);
2+
let arr1 = num1.split('+'),
3+
arr2 = num2.split('+');
4+
let r1 = Number(arr1[0]),
5+
r2 = Number(arr2[0]);
46
let v1 = Number(arr1[1].substring(0, arr1[1].length - 1)),
57
v2 = Number(arr2[1].substring(0, arr2[1].length - 1));
68
let ansR = r1 * r2 - v1 * v2;
79
let ansV = r1 * v2 + r2 * v1;
810
return `${ansR}+${ansV}i`;
9-
};
11+
}

solution/0600-0699/0688.Knight Probability in Chessboard/Solution.ts

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1-
function knightProbability(n: number, k: number, row: number, column: number): number {
2-
let dp = Array.from({ length: k + 1 }, v => Array.from({ length: n }, w => new Array(n).fill(0)));
3-
const directions = [[-2, -1], [-2, 1], [-1, -2], [-1, 2], [1, -2], [1, 2], [2, -1], [2, 1]];
1+
function knightProbability(
2+
n: number,
3+
k: number,
4+
row: number,
5+
column: number,
6+
): number {
7+
let dp = Array.from({ length: k + 1 }, v =>
8+
Array.from({ length: n }, w => new Array(n).fill(0)),
9+
);
10+
const directions = [
11+
[-2, -1],
12+
[-2, 1],
13+
[-1, -2],
14+
[-1, 2],
15+
[1, -2],
16+
[1, 2],
17+
[2, -1],
18+
[2, 1],
19+
];
420
for (let depth = 0; depth <= k; depth++) {
521
for (let i = 0; i < n; i++) {
622
for (let j = 0; j < n; j++) {
@@ -14,9 +30,8 @@ function knightProbability(n: number, k: number, row: number, column: number): n
1430
}
1531
}
1632
}
17-
1833
}
1934
}
2035
}
2136
return dp[k][row][column];
22-
};
37+
}

solution/0700-0799/0712.Minimum ASCII Delete Sum for Two Strings/Solution.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def minimumDeleteSum(self, s1: str, s2: str) -> int:
1111
if s1[i - 1] == s2[j - 1]:
1212
dp[i][j] = dp[i - 1][j - 1]
1313
else:
14-
dp[i][j] = min(dp[i - 1][j] + ord(s1[i - 1]),
15-
dp[i][j - 1] + ord(s2[j - 1]))
14+
dp[i][j] = min(
15+
dp[i - 1][j] + ord(s1[i - 1]), dp[i][j - 1] + ord(s2[j - 1])
16+
)
1617
return dp[-1][-1]

0 commit comments

Comments
 (0)