Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ function reverseBetween(head: ListNode | null, left: number, right: number): Lis
prev1 = tail
tail = next
}
if (prev !== null) {
prev.next = prev1
} else {
if (prev === null) {
head = prev1
} else {
prev.next = prev1
}
if (start !== null) {
start.next = tail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function isInterleave(s1: string, s2: string, s3: string): boolean {
if (s3.length !== s1.length + s2.length) {
return false
}
const cache: boolean[][] = Array.from({ length: s1.length + 1 }, () => Array(s2.length + 1).fill(null))
const cache: boolean[][] = Array.from({ length: s1.length + 1 }, () => new Array(s2.length + 1).fill(null))
return isInterleaveHelper(s1, s2, s3, 0, 0, 0, cache)
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/ts/g0101_0200/s0101_symmetric_tree/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ function isSymmetric(root: TreeNode | null): boolean {
if (!leftNode || !rightNode || leftNode.val != rightNode.val) {
return false
}
queue.push([leftNode.left, rightNode.right])
queue.push([leftNode.right, rightNode.left])
queue.push([leftNode.left, rightNode.right], [leftNode.right, rightNode.left])
}
}
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,31 @@ import { TreeNode } from '../../com_github_leetcode/treenode'
* }
*/
function zigzagLevelOrder(root: TreeNode | null): number[][] {
if (!root) return []

const result: number[][] = []
if (root === null) {
return result
}
const q: (TreeNode | null)[] = [root, null]
let zig = true
let level: number[] = []
while (q.length > 0) {
const node = q.shift()
if (node === null) {
result.push(level)
zig = !zig
level = []
if (q.length > 0) {
q.push(null)
}
} else {
if (zig) {
level.push(node.val)
} else {
level.unshift(node.val)
}
if (node.left !== null) {
q.push(node.left)
}
if (node.right !== null) {
q.push(node.right)
}
const queue: TreeNode[] = [root]
let leftToRight = true

while (queue.length) {
const size = queue.length
const level = new Array<number>(size)

for (let i = 0; i < size; i++) {
const node = queue.shift()!
const index = leftToRight ? i : size - 1 - i
level[index] = node.val

if (node.left) queue.push(node.left)
if (node.right) queue.push(node.right)
}

result.push(level)
leftToRight = !leftToRight
}

return result
}


export { zigzagLevelOrder }
2 changes: 1 addition & 1 deletion src/main/ts/g0101_0200/s0120_triangle/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function minimumTotal(triangle: number[][]): number {
}
const rows = triangle.length
const cols = triangle[rows - 1].length
const dp: number[][] = Array.from({ length: rows }, () => Array(cols).fill(-10001))
const dp: number[][] = Array.from({ length: rows }, () => new Array(cols).fill(-10001))
function dfs(row: number, col: number): number {
if (row >= triangle.length) {
return 0
Expand Down
2 changes: 1 addition & 1 deletion src/main/ts/g0101_0200/s0125_valid_palindrome/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function isPalindrome(s: string): boolean {
if (s.length < 2) {
return true
}
let sFormated = s.toLowerCase().replace(/[^a-zA-Z0-9]/g, '')
let sFormated = s.toLowerCase().replaceAll(/[^a-zA-Z0-9]/g, '')
let reversed = sFormated.split('').reverse().join('').replace(',', '')
return sFormated === reversed
}
Expand Down
59 changes: 29 additions & 30 deletions src/main/ts/g0101_0200/s0127_word_ladder/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,40 @@
// #2025_04_07_Time_41_ms_(95.63%)_Space_63.50_MB_(82.78%)

function ladderLength(beginWord: string, endWord: string, wordList: string[]): number {
const wordSet: Set<string> = new Set(wordList)
if (!wordSet.has(endWord)) {
return 0
}
let beginSet: Set<string> = new Set([beginWord])
let endSet: Set<string> = new Set([endWord])
const visited: Set<string> = new Set()
let len = 1
const wordLen = beginWord.length
while (beginSet.size > 0 && endSet.size > 0) {
if (beginSet.size > endSet.size) {
;[beginSet, endSet] = [endSet, beginSet]
}
const tempSet: Set<string> = new Set()
for (const word of beginSet) {
const chars = word.split('')
for (let i = 0; i < wordLen; i++) {
const oldChar = chars[i]
for (let c = 97; c <= 122; c++) {
chars[i] = String.fromCharCode(c)
const nextWord = chars.join('')
if (endSet.has(nextWord)) {
return len + 1
}
if (!visited.has(nextWord) && wordSet.has(nextWord)) {
tempSet.add(nextWord)
visited.add(nextWord)
const wordSet = new Set(wordList)
if (!wordSet.has(endWord)) return 0

const queue = [beginWord]
let steps = 1

while (queue.length > 0) {
const levelSize = queue.length

for (let i = 0; i < levelSize; i++) {
const word = queue.shift()!

// Try changing each letter
for (let pos = 0; pos < word.length; pos++) {
for (let charCode = 97; charCode <= 122; charCode++) {
// 'a' to 'z'
const newChar = String.fromCodePoint(charCode)
if (newChar === word[pos]) continue

const newWord = word.slice(0, pos) + newChar + word.slice(pos + 1)

if (newWord === endWord) return steps + 1

if (wordSet.has(newWord)) {
queue.push(newWord)
wordSet.delete(newWord)
}
}
chars[i] = oldChar
}
}
beginSet = tempSet
len++

steps++
}

return 0
}

Expand Down
57 changes: 26 additions & 31 deletions src/main/ts/g0101_0200/s0130_surrounded_regions/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,42 @@
Do not return anything, modify board in-place instead.
*/
function solve(board: string[][]): void {
if (board.length === 0) {
return
}
if (!board.length) return

const rows = board.length
const cols = board[0].length
const dfs = (board: string[][], row: number, col: number): void => {
if (row < 0 || row >= rows || col < 0 || col >= cols || board[row][col] !== 'O') {
return
}
board[row][col] = '#'
dfs(board, row + 1, col)
dfs(board, row - 1, col)
dfs(board, row, col + 1)
dfs(board, row, col - 1)
}
for (let i = 0; i < cols; i++) {
if (board[0][i] === 'O') {
dfs(board, 0, i)
}
if (board[rows - 1][i] === 'O') {
dfs(board, rows - 1, i)
const dirs = [[1,0], [-1,0], [0,1], [0,-1]]

const dfs = (r: number, c: number): void => {
if (
r < 0 || r >= rows ||
c < 0 || c >= cols ||
board[r][c] !== 'O'
) return

board[r][c] = '#'
for (const [dr, dc] of dirs) {
dfs(r + dr, c + dc)
}
}

// mark border-connected O's
for (let i = 0; i < rows; i++) {
if (board[i][0] === 'O') {
dfs(board, i, 0)
}
if (board[i][cols - 1] === 'O') {
dfs(board, i, cols - 1)
}
dfs(i, 0)
dfs(i, cols - 1)
}
for (let j = 0; j < cols; j++) {
dfs(0, j)
dfs(rows - 1, j)
}

// flip & restore
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (board[i][j] === 'O') {
board[i][j] = 'X'
}
if (board[i][j] === '#') {
board[i][j] = 'O'
}
board[i][j] = board[i][j] === '#' ? 'O' : 'X'
}
}
}


export { solve }
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const copyRandomList = (head: Node | null): Node | null => {
while (cursor !== null && newCursor !== null) {
if (cursor.random !== null) {
const targetNode = map.get(cursor.random)
if (typeof targetNode !== 'undefined') {
if (targetNode !== undefined) {
newCursor.random = targetNode
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class BSTIterator {
next(): number {
let res = -1
while (this.node !== null) {
if (this.node.left !== null) {
if (this.node.left === null) {
res = this.node.val
this.node = this.node.right
return res
} else {
let rightMost = this.node.left
while (rightMost.right !== null && rightMost.right !== this.node) {
rightMost = rightMost.right
Expand All @@ -42,10 +46,6 @@ class BSTIterator {
this.node = this.node.right
return res
}
} else {
res = this.node.val
this.node = this.node.right
return res
}
}
return res
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Trie {
}

private getCharIndex(char: string): number {
return char.charCodeAt(0) - 'a'.charCodeAt(0)
return char.codePointAt(0)! - 'a'.codePointAt(0)!
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class WordDictionary {

addWord(word: string): void {
let current = this.root
for (let i = 0; i < word.length; i++) {
const letter = word[i]
for (const letter of word) {
if (!current.hasOwnProperty(letter)) {
current[letter] = {}
}
Expand All @@ -33,12 +32,10 @@ class WordDictionary {
index += 1
if (currentLetter === '.') {
return Object.keys(subtree).some((letter) => searchSubtree(word, index, subtree[letter] as TrieNode))
} else if (subtree.hasOwnProperty(currentLetter)) {
return searchSubtree(word, index, subtree[currentLetter] as TrieNode)
} else {
if (subtree.hasOwnProperty(currentLetter)) {
return searchSubtree(word, index, subtree[currentLetter] as TrieNode)
} else {
return false
}
return false
}
}
return searchSubtree(word, 0, this.root)
Expand Down
45 changes: 24 additions & 21 deletions src/main/ts/g0201_0300/s0224_basic_calculator/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,37 @@ function calculate(s: string): number {
let i = 0

function helper(ca: string[]): number {
let result = 0
let num = 0
let prenum = 0
let isPlus = true
let sign = 1

while (i < ca.length) {
const c = ca[i]
if (c !== ' ') {
if (c >= '0' && c <= '9') {
num = num * 10 + parseInt(c)
} else if (c === '+') {
prenum += num * (isPlus ? 1 : -1)
isPlus = true
num = 0
} else if (c === '-') {
prenum += num * (isPlus ? 1 : -1)
isPlus = false
num = 0
} else if (c === '(') {
i++
num = helper(ca)
} else if (c === ')') {
prenum += num * (isPlus ? 1 : -1)
return prenum
}

if (c === ' ') {
i++
continue
}

if (c >= '0' && c <= '9') {
num = num * 10 + (c.codePointAt(0)! - 48)
} else if (c === '+' || c === '-') {
result += sign * num
sign = c === '+' ? 1 : -1
num = 0
} else if (c === '(') {
i++
num = helper(ca)
} else if (c === ')') {
return result + sign * num
}

i++
}
return prenum + num * (isPlus ? 1 : -1)

return result + sign * num
}

return helper(s.split(''))
}

Expand Down
Loading