Skip to content

Commit 24ae5ab

Browse files
committed
Improved tasks
1 parent 01eaf60 commit 24ae5ab

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/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ You may assume the two numbers do not contain any leading zero, except the numbe
4242
```typescript
4343
import { ListNode } from '../../com_github_leetcode/listnode'
4444

45-
/*
45+
/**
4646
* Definition for singly-linked list.
4747
* class ListNode {
4848
* val: number

src/main/ts/g0001_0100/s0023_merge_k_sorted_lists/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ _Merge all the linked-lists into one sorted linked-list and return it._
4343
```typescript
4444
import { ListNode } from '../../com_github_leetcode/listnode'
4545

46-
/*
46+
/**
4747
* Definition for singly-linked list.
4848
* class ListNode {
4949
* val: number

src/main/ts/g0001_0100/s0030_substring_with_concatenation_of_all_words/readme.md

-6
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,26 @@ function findSubstring(s: string, words: string[]): number[] {
4545
let n1 = words[0].length
4646
let n2 = s.length
4747
let map1 = new Map<string, number>()
48-
4948
for (let ch of words) {
5049
map1.set(ch, (map1.get(ch) ?? 0) + 1)
5150
}
52-
5351
for (let i = 0; i < n1; i++) {
5452
let left = i
5553
let j = i
5654
let c = 0
5755
let map2 = new Map<string, number>()
58-
5956
while (j + n1 <= n2) {
6057
let word1 = s.substring(j, j + n1)
6158
j += n1
62-
6359
if (map1.has(word1)) {
6460
map2.set(word1, (map2.get(word1) ?? 0) + 1)
6561
c++
66-
6762
while ((map2.get(word1) ?? 0) > (map1.get(word1) ?? 0)) {
6863
let word2 = s.substring(left, left + n1)
6964
map2.set(word2, (map2.get(word2) ?? 0) - 1)
7065
left += n1
7166
c--
7267
}
73-
7468
if (c === words.length) {
7569
ans.push(left)
7670
}

src/main/ts/g0001_0100/s0031_next_permutation/readme.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,14 @@ The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algor
5050
*/
5151
function nextPermutation(nums: number[]): void {
5252
let swapperIndex: number | null = null
53-
5453
for (let ind = nums.length - 1; ind >= 0 && swapperIndex == null; ind--) {
5554
if (nums[ind] > nums[ind - 1]) {
5655
swapperIndex = ind - 1
5756
}
5857
}
59-
60-
if (swapperIndex == null) nums.sort((a, b) => a - b)
61-
else {
58+
if (swapperIndex == null) {
59+
nums.sort((a, b) => a - b)
60+
} else {
6261
nums.splice(swapperIndex + 1, nums.length, ...nums.slice(swapperIndex + 1, nums.length).sort((a, b) => a - b))
6362
let indToBringForward = swapperIndex + 1
6463
while (nums[indToBringForward] <= nums[swapperIndex]) ++indToBringForward

src/main/ts/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/readme.md

-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ function searchRange(nums: number[], target: number): number[] { //NOSONAR
4646
let left2 = left1
4747
let right1 = nums.length - 1
4848
let right2 = right1
49-
5049
while (left1 <= right1 || left2 <= right2) {
5150
if (left1 <= right1) {
5251
let mid1 = Math.floor((left1 + right1) / 2)
@@ -59,7 +58,6 @@ function searchRange(nums: number[], target: number): number[] { //NOSONAR
5958
right1 = mid1 - 1
6059
}
6160
}
62-
6361
if (left2 <= right2) {
6462
let mid2 = Math.floor((left2 + right2) / 2)
6563
if (nums[mid2] == target) {

src/main/ts/g0001_0100/s0036_valid_sudoku/readme.md

-3
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,16 @@ function isValidSudoku(board: string[][]): boolean {
6767
let rowSet: number[] = new Array(9).fill(0)
6868
let colSet: number[] = new Array(9).fill(0)
6969
let boxSet: number[] = new Array(9).fill(0)
70-
7170
for (let i = 0; i < 9; i++) {
7271
for (let j = 0; j < 9; j++) {
7372
if (board[i][j] === '.') {
7473
continue
7574
}
7675
let val = board[i][j].charCodeAt(0) - '0'.charCodeAt(0)
7776
let boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3)
78-
7977
if (rowSet[i] & (1 << val) || colSet[j] & (1 << val) || boxSet[boxIndex] & (1 << val)) {
8078
return false
8179
}
82-
8380
rowSet[i] |= 1 << val
8481
colSet[j] |= 1 << val
8582
boxSet[boxIndex] |= 1 << val

src/main/ts/g0001_0100/s0039_combination_sum/readme.md

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ It is **guaranteed** that the number of unique combinations that sum up to `targ
6060
function combinationSum(candidates: number[], target: number): number[][] {
6161
const result: number[][] = []
6262
const path: number[] = []
63-
6463
const comFunct = (index: number, sum: number) => {
6564
if (sum === target) {
6665
result.push([...path])

src/main/ts/g0001_0100/s0054_spiral_matrix/readme.md

-5
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,24 @@ function spiralOrder(matrix: number[][]): number[] {
3939
c = 0
4040
let bigR = matrix.length - 1
4141
let bigC = matrix[0].length - 1
42-
4342
while (r <= bigR && c <= bigC) {
4443
for (let i = c; i <= bigC; i++) {
4544
result.push(matrix[r][i])
4645
}
4746
r++
48-
4947
for (let i = r; i <= bigR; i++) {
5048
result.push(matrix[i][bigC])
5149
}
5250
bigC--
53-
5451
for (let i = bigC; i >= c && r <= bigR; i--) {
5552
result.push(matrix[bigR][i])
5653
}
5754
bigR--
58-
5955
for (let i = bigR; i >= r && c <= bigC; i--) {
6056
result.push(matrix[i][c])
6157
}
6258
c++
6359
}
64-
6560
return result
6661
}
6762

src/main/ts/g0001_0100/s0070_climbing_stairs/readme.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ Each time you can either climb `1` or `2` steps. In how many distinct ways can y
3333

3434
```typescript
3535
function climbStairs(n: number, memo: Record<string, number> = {}): number {
36-
if (n in memo) return memo[n]
37-
if (n === 0) return 1
38-
if (n < 0) return 0
36+
if (n in memo) {
37+
return memo[n]
38+
}
39+
if (n === 0) {
40+
return 1
41+
}
42+
if (n < 0) {
43+
return 0
44+
}
3945
memo[n] = climbStairs(n - 1, memo) + climbStairs(n - 2, memo)
4046
return memo[n]
4147
}

src/main/ts/g0001_0100/s0072_edit_distance/readme.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ function minDistance(word1: string, word2: string): number {
4242
const l1 = word1.length
4343
const l2 = word2.length
4444
const dfs = (w1: number, w2: number): number => {
45-
if (memo[w1][w2] != undefined) return memo[w1][w2]
45+
if (memo[w1][w2] != undefined) {
46+
return memo[w1][w2]
47+
}
4648
if (w1 == l1 && w2 == l2) {
4749
memo[w1][w2] = 0
4850
return 0

src/main/ts/g0001_0100/s0078_subsets/readme.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ The solution set **must not** contain duplicate subsets. Return the solution in
3232
```typescript
3333
function subsets(nums: number[]): number[][] {
3434
const sets: number[][] = [[]]
35-
for (const num of nums) sets.push(...sets.map((set) => [...set, num]))
35+
for (const num of nums) {
36+
sets.push(...sets.map((set) => [...set, num]))
37+
}
3638
return sets
3739
}
3840

src/main/ts/g0001_0100/s0079_word_search/readme.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ function exist(board: string[][], word: string): boolean {
5656
if (loop(marks, board, i, j, word, 0)) return true
5757
}
5858
}
59-
6059
return ret
6160
}
6261

@@ -75,28 +74,29 @@ function loop(marks: boolean[][], board: string[][], i: number, j: number, word:
7574
if (i < 0 || j < 0 || i >= board.length || j >= board[i].length || marks[i][j]) {
7675
return false
7776
}
78-
7977
if (board[i][j] !== word.charAt(index)) {
8078
return false
8179
} else if (index === word.length - 1) {
8280
return true
8381
}
84-
8582
marks[i][j] = true
8683
index++
87-
8884
let r = loop(marks, board, i - 1, j, word, index)
89-
if (r) return true
90-
85+
if (r) {
86+
return true
87+
}
9188
r = loop(marks, board, i + 1, j, word, index)
92-
if (r) return true
93-
89+
if (r) {
90+
return true
91+
}
9492
r = loop(marks, board, i, j - 1, word, index)
95-
if (r) return true
96-
93+
if (r) {
94+
return true
95+
}
9796
r = loop(marks, board, i, j + 1, word, index)
98-
if (r) return true
99-
97+
if (r) {
98+
return true
99+
}
100100
marks[i][j] = false
101101
return false
102102
}

src/main/ts/g0001_0100/s0094_binary_tree_inorder_traversal/readme.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Given the `root` of a binary tree, return _the inorder traversal of its nodes' v
5555
```typescript
5656
import { TreeNode } from '../../com_github_leetcode/treenode'
5757

58-
/*
58+
/**
5959
* Definition for a binary tree node.
6060
* class TreeNode {
6161
* val: number
@@ -69,8 +69,12 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
6969
* }
7070
*/
7171
function inorderTraversal(root: TreeNode | null): number[] {
72-
if (!root) return []
73-
if (!root.val) return []
72+
if (!root) {
73+
return []
74+
}
75+
if (!root.val) {
76+
return []
77+
}
7478
const result: number[] = []
7579
function traverse(node: TreeNode, arr: number[]) {
7680
if (node.left) {

src/main/ts/g0001_0100/s0098_validate_binary_search_tree/readme.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
5555
* }
5656
*/
5757
function dfs(node: TreeNode | null, lowerBound: number, upperBound: number): boolean {
58-
if (!node) return true
59-
if (node.val <= lowerBound) return false
60-
if (node.val >= upperBound) return false
58+
if (!node) {
59+
return true
60+
}
61+
if (node.val <= lowerBound) {
62+
return false
63+
}
64+
if (node.val >= upperBound) {
65+
return false
66+
}
6167
return dfs(node.left, lowerBound, node.val) && dfs(node.right, node.val, upperBound)
6268
}
6369

src/main/ts/g0101_0200/s0101_symmetric_tree/readme.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e
3535
```typescript
3636
import { TreeNode } from '../../com_github_leetcode/treenode'
3737

38-
/*
38+
/**
3939
* Definition for a binary tree node.
4040
* class TreeNode {
4141
* val: number
@@ -49,14 +49,20 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
4949
* }
5050
*/
5151
function isSymmetric(root: TreeNode | null): boolean {
52-
if (!root.left && !root.right) return true
52+
if (!root.left && !root.right) {
53+
return true
54+
}
5355
const queue: [TreeNode, TreeNode][] = [[root.left, root.right]]
5456
while (queue.length > 0) {
5557
let qLen: number = queue.length
5658
while (qLen-- > 0) {
5759
const [leftNode, rightNode] = queue.shift()
58-
if (!leftNode && !rightNode) continue
59-
if (!leftNode || !rightNode || leftNode.val != rightNode.val) return false
60+
if (!leftNode && !rightNode) {
61+
continue
62+
}
63+
if (!leftNode || !rightNode || leftNode.val != rightNode.val) {
64+
return false
65+
}
6066
queue.push([leftNode.left, rightNode.right])
6167
queue.push([leftNode.right, rightNode.left])
6268
}

src/main/ts/g0101_0200/s0102_binary_tree_level_order_traversal/readme.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Given the `root` of a binary tree, return _the level order traversal of its node
3737
```typescript
3838
import { TreeNode } from '../../com_github_leetcode/treenode'
3939

40-
/*
40+
/**
4141
* Definition for a binary tree node.
4242
* class TreeNode {
4343
* val: number
@@ -51,7 +51,9 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
5151
* }
5252
*/
5353
function levelOrder(root: TreeNode | null): number[][] {
54-
if (root == null) return []
54+
if (root == null) {
55+
return []
56+
}
5557
let queue = [root]
5658
let result = []
5759
while (queue.length != 0) {
@@ -60,8 +62,12 @@ function levelOrder(root: TreeNode | null): number[][] {
6062
while (length > 0) {
6163
let node = queue.shift()
6264
subResult.push(node.val)
63-
if (node.left != null) queue.push(node.left)
64-
if (node.right != null) queue.push(node.right)
65+
if (node.left != null) {
66+
queue.push(node.left)
67+
}
68+
if (node.right != null) {
69+
queue.push(node.right)
70+
}
6571
length--
6672
}
6773
result.push(subResult)

src/main/ts/g0101_0200/s0104_maximum_depth_of_binary_tree/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ A binary tree's **maximum depth** is the number of nodes along the longest path
4545
```typescript
4646
import { TreeNode } from '../../com_github_leetcode/treenode'
4747

48-
/*
48+
/**
4949
* Definition for a binary tree node.
5050
* class TreeNode {
5151
* val: number

src/main/ts/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Given two integer arrays `preorder` and `inorder` where `preorder` is the preord
3636
```typescript
3737
import { TreeNode } from '../../com_github_leetcode/treenode'
3838

39-
/*
39+
/**
4040
* Definition for a binary tree node.
4141
* class TreeNode {
4242
* val: number

src/main/ts/g0101_0200/s0114_flatten_binary_tree_to_linked_list/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Given the `root` of a binary tree, flatten the tree into a "linked list":
4242
```typescript
4343
import { TreeNode } from '../../com_github_leetcode/treenode'
4444

45-
/*
45+
/**
4646
* Definition for a binary tree node.
4747
* class TreeNode {
4848
* val: number

src/main/ts/g0101_0200/s0124_binary_tree_maximum_path_sum/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Given the `root` of a binary tree, return _the maximum **path sum** of any **non
4141
```typescript
4242
import { TreeNode } from '../../com_github_leetcode/treenode'
4343

44-
/*
44+
/**
4545
* Definition for a binary tree node.
4646
* class TreeNode {
4747
* val: number

src/main/ts/g0101_0200/s0138_copy_list_with_random_pointer/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Your code will **only** be given the `head` of the original linked list.
6363
```typescript
6464
import { Node } from '../../com_github_leetcode/node'
6565

66-
/*
66+
/**
6767
* Definition for Node.
6868
* class Node {
6969
* val: number

0 commit comments

Comments
 (0)