Skip to content

Improved tasks #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/ts/g0001_0100/s0002_add_two_numbers/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

/*
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

/*
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,26 @@ function findSubstring(s: string, words: string[]): number[] {
let n1 = words[0].length
let n2 = s.length
let map1 = new Map<string, number>()

for (let ch of words) {
map1.set(ch, (map1.get(ch) ?? 0) + 1)
}

for (let i = 0; i < n1; i++) {
let left = i
let j = i
let c = 0
let map2 = new Map<string, number>()

while (j + n1 <= n2) {
let word1 = s.substring(j, j + n1)
j += n1

if (map1.has(word1)) {
map2.set(word1, (map2.get(word1) ?? 0) + 1)
c++

while ((map2.get(word1) ?? 0) > (map1.get(word1) ?? 0)) {
let word2 = s.substring(left, left + n1)
map2.set(word2, (map2.get(word2) ?? 0) - 1)
left += n1
c--
}

if (c === words.length) {
ans.push(left)
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/ts/g0001_0100/s0031_next_permutation/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
*/
function nextPermutation(nums: number[]): void {
let swapperIndex: number | null = null

for (let ind = nums.length - 1; ind >= 0 && swapperIndex == null; ind--) {
if (nums[ind] > nums[ind - 1]) {
swapperIndex = ind - 1
}
}

if (swapperIndex == null) nums.sort((a, b) => a - b)
else {
if (swapperIndex == null) {
nums.sort((a, b) => a - b)
} else {
nums.splice(swapperIndex + 1, nums.length, ...nums.slice(swapperIndex + 1, nums.length).sort((a, b) => a - b))
let indToBringForward = swapperIndex + 1
while (nums[indToBringForward] <= nums[swapperIndex]) ++indToBringForward
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ function searchRange(nums: number[], target: number): number[] { //NOSONAR
let left2 = left1
let right1 = nums.length - 1
let right2 = right1

while (left1 <= right1 || left2 <= right2) {
if (left1 <= right1) {
let mid1 = Math.floor((left1 + right1) / 2)
Expand All @@ -22,7 +21,6 @@ function searchRange(nums: number[], target: number): number[] { //NOSONAR
right1 = mid1 - 1
}
}

if (left2 <= right2) {
let mid2 = Math.floor((left2 + right2) / 2)
if (nums[mid2] == target) {
Expand Down
3 changes: 0 additions & 3 deletions src/main/ts/g0001_0100/s0036_valid_sudoku/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ function isValidSudoku(board: string[][]): boolean {
let rowSet: number[] = new Array(9).fill(0)
let colSet: number[] = new Array(9).fill(0)
let boxSet: number[] = new Array(9).fill(0)

for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
if (board[i][j] === '.') {
continue
}
let val = board[i][j].charCodeAt(0) - '0'.charCodeAt(0)
let boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3)

if (rowSet[i] & (1 << val) || colSet[j] & (1 << val) || boxSet[boxIndex] & (1 << val)) {
return false
}

rowSet[i] |= 1 << val
colSet[j] |= 1 << val
boxSet[boxIndex] |= 1 << val
Expand Down
1 change: 0 additions & 1 deletion src/main/ts/g0001_0100/s0039_combination_sum/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
function combinationSum(candidates: number[], target: number): number[][] {
const result: number[][] = []
const path: number[] = []

const comFunct = (index: number, sum: number) => {
if (sum === target) {
result.push([...path])
Expand Down
5 changes: 0 additions & 5 deletions src/main/ts/g0001_0100/s0054_spiral_matrix/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,24 @@ function spiralOrder(matrix: number[][]): number[] {
c = 0
let bigR = matrix.length - 1
let bigC = matrix[0].length - 1

while (r <= bigR && c <= bigC) {
for (let i = c; i <= bigC; i++) {
result.push(matrix[r][i])
}
r++

for (let i = r; i <= bigR; i++) {
result.push(matrix[i][bigC])
}
bigC--

for (let i = bigC; i >= c && r <= bigR; i--) {
result.push(matrix[bigR][i])
}
bigR--

for (let i = bigR; i >= r && c <= bigC; i--) {
result.push(matrix[i][c])
}
c++
}

return result
}

Expand Down
12 changes: 9 additions & 3 deletions src/main/ts/g0001_0100/s0070_climbing_stairs/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
// #Big_O_Time_O(n)_Space_O(n) #2025_03_23_Time_0_ms_(100.00%)_Space_56.23_MB_(5.15%)

function climbStairs(n: number, memo: Record<string, number> = {}): number {
if (n in memo) return memo[n]
if (n === 0) return 1
if (n < 0) return 0
if (n in memo) {
return memo[n]
}
if (n === 0) {
return 1
}
if (n < 0) {
return 0
}
memo[n] = climbStairs(n - 1, memo) + climbStairs(n - 2, memo)
return memo[n]
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/ts/g0001_0100/s0072_edit_distance/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ function minDistance(word1: string, word2: string): number {
const l1 = word1.length
const l2 = word2.length
const dfs = (w1: number, w2: number): number => {
if (memo[w1][w2] != undefined) return memo[w1][w2]
if (memo[w1][w2] != undefined) {
return memo[w1][w2]
}
if (w1 == l1 && w2 == l2) {
memo[w1][w2] = 0
return 0
Expand Down
4 changes: 3 additions & 1 deletion src/main/ts/g0001_0100/s0078_subsets/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

function subsets(nums: number[]): number[][] {
const sets: number[][] = [[]]
for (const num of nums) sets.push(...sets.map((set) => [...set, num]))
for (const num of nums) {
sets.push(...sets.map((set) => [...set, num]))
}
return sets
}

Expand Down
24 changes: 12 additions & 12 deletions src/main/ts/g0001_0100/s0079_word_search/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ function exist(board: string[][], word: string): boolean {
if (loop(marks, board, i, j, word, 0)) return true
}
}

return ret
}

Expand All @@ -31,28 +30,29 @@ function loop(marks: boolean[][], board: string[][], i: number, j: number, word:
if (i < 0 || j < 0 || i >= board.length || j >= board[i].length || marks[i][j]) {
return false
}

if (board[i][j] !== word.charAt(index)) {
return false
} else if (index === word.length - 1) {
return true
}

marks[i][j] = true
index++

let r = loop(marks, board, i - 1, j, word, index)
if (r) return true

if (r) {
return true
}
r = loop(marks, board, i + 1, j, word, index)
if (r) return true

if (r) {
return true
}
r = loop(marks, board, i, j - 1, word, index)
if (r) return true

if (r) {
return true
}
r = loop(marks, board, i, j + 1, word, index)
if (r) return true

if (r) {
return true
}
marks[i][j] = false
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

/*
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
Expand All @@ -18,8 +18,12 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
* }
*/
function inorderTraversal(root: TreeNode | null): number[] {
if (!root) return []
if (!root.val) return []
if (!root) {
return []
}
if (!root.val) {
return []
}
const result: number[] = []
function traverse(node: TreeNode, arr: number[]) {
if (node.left) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
* }
*/
function dfs(node: TreeNode | null, lowerBound: number, upperBound: number): boolean {
if (!node) return true
if (node.val <= lowerBound) return false
if (node.val >= upperBound) return false
if (!node) {
return true
}
if (node.val <= lowerBound) {
return false
}
if (node.val >= upperBound) {
return false
}
return dfs(node.left, lowerBound, node.val) && dfs(node.right, node.val, upperBound)
}

Expand Down
14 changes: 10 additions & 4 deletions src/main/ts/g0101_0200/s0101_symmetric_tree/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

/*
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
Expand All @@ -19,14 +19,20 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
* }
*/
function isSymmetric(root: TreeNode | null): boolean {
if (!root.left && !root.right) return true
if (!root.left && !root.right) {
return true
}
const queue: [TreeNode, TreeNode][] = [[root.left, root.right]]
while (queue.length > 0) {
let qLen: number = queue.length
while (qLen-- > 0) {
const [leftNode, rightNode] = queue.shift()
if (!leftNode && !rightNode) continue
if (!leftNode || !rightNode || leftNode.val != rightNode.val) return false
if (!leftNode && !rightNode) {
continue
}
if (!leftNode || !rightNode || leftNode.val != rightNode.val) {
return false
}
queue.push([leftNode.left, rightNode.right])
queue.push([leftNode.right, rightNode.left])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

/*
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
Expand All @@ -19,7 +19,9 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
* }
*/
function levelOrder(root: TreeNode | null): number[][] {
if (root == null) return []
if (root == null) {
return []
}
let queue = [root]
let result = []
while (queue.length != 0) {
Expand All @@ -28,8 +30,12 @@ function levelOrder(root: TreeNode | null): number[][] {
while (length > 0) {
let node = queue.shift()
subResult.push(node.val)
if (node.left != null) queue.push(node.left)
if (node.right != null) queue.push(node.right)
if (node.left != null) {
queue.push(node.left)
}
if (node.right != null) {
queue.push(node.right)
}
length--
}
result.push(subResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

/*
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

/*
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

/*
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

/*
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

/*
/**
* Definition for Node.
* class Node {
* val: number
Expand Down
Loading