Skip to content

Commit ca22e0f

Browse files
authored
Improved tasks
1 parent f5e5230 commit ca22e0f

File tree

37 files changed

+106
-85
lines changed

37 files changed

+106
-85
lines changed

src/main/ts/g0001_0100/s0002_add_two_numbers/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { ListNode } from '../../com_github_leetcode/listnode'
77

8-
/*
8+
/**
99
* Definition for singly-linked list.
1010
* class ListNode {
1111
* val: number

src/main/ts/g0001_0100/s0023_merge_k_sorted_lists/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { ListNode } from '../../com_github_leetcode/listnode'
66

7-
/*
7+
/**
88
* Definition for singly-linked list.
99
* class ListNode {
1010
* val: number

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,26 @@ function findSubstring(s: string, words: string[]): number[] {
66
let n1 = words[0].length
77
let n2 = s.length
88
let map1 = new Map<string, number>()
9-
109
for (let ch of words) {
1110
map1.set(ch, (map1.get(ch) ?? 0) + 1)
1211
}
13-
1412
for (let i = 0; i < n1; i++) {
1513
let left = i
1614
let j = i
1715
let c = 0
1816
let map2 = new Map<string, number>()
19-
2017
while (j + n1 <= n2) {
2118
let word1 = s.substring(j, j + n1)
2219
j += n1
23-
2420
if (map1.has(word1)) {
2521
map2.set(word1, (map2.get(word1) ?? 0) + 1)
2622
c++
27-
2823
while ((map2.get(word1) ?? 0) > (map1.get(word1) ?? 0)) {
2924
let word2 = s.substring(left, left + n1)
3025
map2.set(word2, (map2.get(word2) ?? 0) - 1)
3126
left += n1
3227
c--
3328
}
34-
3529
if (c === words.length) {
3630
ans.push(left)
3731
}

src/main/ts/g0001_0100/s0031_next_permutation/solution.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
*/
77
function nextPermutation(nums: number[]): void {
88
let swapperIndex: number | null = null
9-
109
for (let ind = nums.length - 1; ind >= 0 && swapperIndex == null; ind--) {
1110
if (nums[ind] > nums[ind - 1]) {
1211
swapperIndex = ind - 1
1312
}
1413
}
15-
16-
if (swapperIndex == null) nums.sort((a, b) => a - b)
17-
else {
14+
if (swapperIndex == null) {
15+
nums.sort((a, b) => a - b)
16+
} else {
1817
nums.splice(swapperIndex + 1, nums.length, ...nums.slice(swapperIndex + 1, nums.length).sort((a, b) => a - b))
1918
let indToBringForward = swapperIndex + 1
2019
while (nums[indToBringForward] <= nums[swapperIndex]) ++indToBringForward

src/main/ts/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/solution.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ function searchRange(nums: number[], target: number): number[] { //NOSONAR
99
let left2 = left1
1010
let right1 = nums.length - 1
1111
let right2 = right1
12-
1312
while (left1 <= right1 || left2 <= right2) {
1413
if (left1 <= right1) {
1514
let mid1 = Math.floor((left1 + right1) / 2)
@@ -22,7 +21,6 @@ function searchRange(nums: number[], target: number): number[] { //NOSONAR
2221
right1 = mid1 - 1
2322
}
2423
}
25-
2624
if (left2 <= right2) {
2725
let mid2 = Math.floor((left2 + right2) / 2)
2826
if (nums[mid2] == target) {

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@ function isValidSudoku(board: string[][]): boolean {
55
let rowSet: number[] = new Array(9).fill(0)
66
let colSet: number[] = new Array(9).fill(0)
77
let boxSet: number[] = new Array(9).fill(0)
8-
98
for (let i = 0; i < 9; i++) {
109
for (let j = 0; j < 9; j++) {
1110
if (board[i][j] === '.') {
1211
continue
1312
}
1413
let val = board[i][j].charCodeAt(0) - '0'.charCodeAt(0)
1514
let boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3)
16-
1715
if (rowSet[i] & (1 << val) || colSet[j] & (1 << val) || boxSet[boxIndex] & (1 << val)) {
1816
return false
1917
}
20-
2118
rowSet[i] |= 1 << val
2219
colSet[j] |= 1 << val
2320
boxSet[boxIndex] |= 1 << val

src/main/ts/g0001_0100/s0039_combination_sum/solution.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
function combinationSum(candidates: number[], target: number): number[][] {
77
const result: number[][] = []
88
const path: number[] = []
9-
109
const comFunct = (index: number, sum: number) => {
1110
if (sum === target) {
1211
result.push([...path])

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,24 @@ function spiralOrder(matrix: number[][]): number[] {
88
c = 0
99
let bigR = matrix.length - 1
1010
let bigC = matrix[0].length - 1
11-
1211
while (r <= bigR && c <= bigC) {
1312
for (let i = c; i <= bigC; i++) {
1413
result.push(matrix[r][i])
1514
}
1615
r++
17-
1816
for (let i = r; i <= bigR; i++) {
1917
result.push(matrix[i][bigC])
2018
}
2119
bigC--
22-
2320
for (let i = bigC; i >= c && r <= bigR; i--) {
2421
result.push(matrix[bigR][i])
2522
}
2623
bigR--
27-
2824
for (let i = bigR; i >= r && c <= bigC; i--) {
2925
result.push(matrix[i][c])
3026
}
3127
c++
3228
}
33-
3429
return result
3530
}
3631

src/main/ts/g0001_0100/s0070_climbing_stairs/solution.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
// #Big_O_Time_O(n)_Space_O(n) #2025_03_23_Time_0_ms_(100.00%)_Space_56.23_MB_(5.15%)
55

66
function climbStairs(n: number, memo: Record<string, number> = {}): number {
7-
if (n in memo) return memo[n]
8-
if (n === 0) return 1
9-
if (n < 0) return 0
7+
if (n in memo) {
8+
return memo[n]
9+
}
10+
if (n === 0) {
11+
return 1
12+
}
13+
if (n < 0) {
14+
return 0
15+
}
1016
memo[n] = climbStairs(n - 1, memo) + climbStairs(n - 2, memo)
1117
return memo[n]
1218
}

src/main/ts/g0001_0100/s0072_edit_distance/solution.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ function minDistance(word1: string, word2: string): number {
88
const l1 = word1.length
99
const l2 = word2.length
1010
const dfs = (w1: number, w2: number): number => {
11-
if (memo[w1][w2] != undefined) return memo[w1][w2]
11+
if (memo[w1][w2] != undefined) {
12+
return memo[w1][w2]
13+
}
1214
if (w1 == l1 && w2 == l2) {
1315
memo[w1][w2] = 0
1416
return 0

src/main/ts/g0001_0100/s0078_subsets/solution.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
function subsets(nums: number[]): number[][] {
66
const sets: number[][] = [[]]
7-
for (const num of nums) sets.push(...sets.map((set) => [...set, num]))
7+
for (const num of nums) {
8+
sets.push(...sets.map((set) => [...set, num]))
9+
}
810
return sets
911
}
1012

src/main/ts/g0001_0100/s0079_word_search/solution.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ function exist(board: string[][], word: string): boolean {
1212
if (loop(marks, board, i, j, word, 0)) return true
1313
}
1414
}
15-
1615
return ret
1716
}
1817

@@ -31,28 +30,29 @@ function loop(marks: boolean[][], board: string[][], i: number, j: number, word:
3130
if (i < 0 || j < 0 || i >= board.length || j >= board[i].length || marks[i][j]) {
3231
return false
3332
}
34-
3533
if (board[i][j] !== word.charAt(index)) {
3634
return false
3735
} else if (index === word.length - 1) {
3836
return true
3937
}
40-
4138
marks[i][j] = true
4239
index++
43-
4440
let r = loop(marks, board, i - 1, j, word, index)
45-
if (r) return true
46-
41+
if (r) {
42+
return true
43+
}
4744
r = loop(marks, board, i + 1, j, word, index)
48-
if (r) return true
49-
45+
if (r) {
46+
return true
47+
}
5048
r = loop(marks, board, i, j - 1, word, index)
51-
if (r) return true
52-
49+
if (r) {
50+
return true
51+
}
5352
r = loop(marks, board, i, j + 1, word, index)
54-
if (r) return true
55-
53+
if (r) {
54+
return true
55+
}
5656
marks[i][j] = false
5757
return false
5858
}

src/main/ts/g0001_0100/s0094_binary_tree_inorder_traversal/solution.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { TreeNode } from '../../com_github_leetcode/treenode'
66

7-
/*
7+
/**
88
* Definition for a binary tree node.
99
* class TreeNode {
1010
* val: number
@@ -18,8 +18,12 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
1818
* }
1919
*/
2020
function inorderTraversal(root: TreeNode | null): number[] {
21-
if (!root) return []
22-
if (!root.val) return []
21+
if (!root) {
22+
return []
23+
}
24+
if (!root.val) {
25+
return []
26+
}
2327
const result: number[] = []
2428
function traverse(node: TreeNode, arr: number[]) {
2529
if (node.left) {

src/main/ts/g0001_0100/s0098_validate_binary_search_tree/solution.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
1919
* }
2020
*/
2121
function dfs(node: TreeNode | null, lowerBound: number, upperBound: number): boolean {
22-
if (!node) return true
23-
if (node.val <= lowerBound) return false
24-
if (node.val >= upperBound) return false
22+
if (!node) {
23+
return true
24+
}
25+
if (node.val <= lowerBound) {
26+
return false
27+
}
28+
if (node.val >= upperBound) {
29+
return false
30+
}
2531
return dfs(node.left, lowerBound, node.val) && dfs(node.right, node.val, upperBound)
2632
}
2733

src/main/ts/g0101_0200/s0101_symmetric_tree/solution.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { TreeNode } from '../../com_github_leetcode/treenode'
77

8-
/*
8+
/**
99
* Definition for a binary tree node.
1010
* class TreeNode {
1111
* val: number
@@ -19,14 +19,20 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
1919
* }
2020
*/
2121
function isSymmetric(root: TreeNode | null): boolean {
22-
if (!root.left && !root.right) return true
22+
if (!root.left && !root.right) {
23+
return true
24+
}
2325
const queue: [TreeNode, TreeNode][] = [[root.left, root.right]]
2426
while (queue.length > 0) {
2527
let qLen: number = queue.length
2628
while (qLen-- > 0) {
2729
const [leftNode, rightNode] = queue.shift()
28-
if (!leftNode && !rightNode) continue
29-
if (!leftNode || !rightNode || leftNode.val != rightNode.val) return false
30+
if (!leftNode && !rightNode) {
31+
continue
32+
}
33+
if (!leftNode || !rightNode || leftNode.val != rightNode.val) {
34+
return false
35+
}
3036
queue.push([leftNode.left, rightNode.right])
3137
queue.push([leftNode.right, rightNode.left])
3238
}

src/main/ts/g0101_0200/s0102_binary_tree_level_order_traversal/solution.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { TreeNode } from '../../com_github_leetcode/treenode'
77

8-
/*
8+
/**
99
* Definition for a binary tree node.
1010
* class TreeNode {
1111
* val: number
@@ -19,7 +19,9 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
1919
* }
2020
*/
2121
function levelOrder(root: TreeNode | null): number[][] {
22-
if (root == null) return []
22+
if (root == null) {
23+
return []
24+
}
2325
let queue = [root]
2426
let result = []
2527
while (queue.length != 0) {
@@ -28,8 +30,12 @@ function levelOrder(root: TreeNode | null): number[][] {
2830
while (length > 0) {
2931
let node = queue.shift()
3032
subResult.push(node.val)
31-
if (node.left != null) queue.push(node.left)
32-
if (node.right != null) queue.push(node.right)
33+
if (node.left != null) {
34+
queue.push(node.left)
35+
}
36+
if (node.right != null) {
37+
queue.push(node.right)
38+
}
3339
length--
3440
}
3541
result.push(subResult)

src/main/ts/g0101_0200/s0104_maximum_depth_of_binary_tree/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import { TreeNode } from '../../com_github_leetcode/treenode'
88

9-
/*
9+
/**
1010
* Definition for a binary tree node.
1111
* class TreeNode {
1212
* val: number

src/main/ts/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { TreeNode } from '../../com_github_leetcode/treenode'
66

7-
/*
7+
/**
88
* Definition for a binary tree node.
99
* class TreeNode {
1010
* val: number

src/main/ts/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { TreeNode } from '../../com_github_leetcode/treenode'
66

7-
/*
7+
/**
88
* Definition for a binary tree node.
99
* class TreeNode {
1010
* val: number

src/main/ts/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { TreeNode } from '../../com_github_leetcode/treenode'
66

7-
/*
7+
/**
88
* Definition for a binary tree node.
99
* class TreeNode {
1010
* val: number

src/main/ts/g0101_0200/s0138_copy_list_with_random_pointer/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { Node } from '../../com_github_leetcode/node'
66

7-
/*
7+
/**
88
* Definition for Node.
99
* class Node {
1010
* val: number

0 commit comments

Comments
 (0)