Skip to content

Commit 82113be

Browse files
authored
Improved tasks 2-82
1 parent e3dcf2e commit 82113be

File tree

17 files changed

+168
-150
lines changed

17 files changed

+168
-150
lines changed

src/main/ts/com_github_leetcode/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ class Node {
1010

1111
toString(): string {
1212
const result: string[] = []
13-
let curr: Node | null = this
13+
let curr: Node | null = this //NOSONAR
1414
while (curr !== null) {
1515
const result2: string[] = []
1616
result2.push(String(curr.val))
1717
if (curr.random === null) {
1818
result2.push('null')
1919
} else {
2020
let randomIndex = 0
21-
let curr2: Node | null = this
21+
let curr2: Node | null = this //NOSONAR
2222
while (curr2?.next !== null && curr2 !== curr.random) {
2323
randomIndex++
2424
curr2 = curr2.next

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | nul
2323
let curr: ListNode | null = dummyHead
2424
let carry: number = 0
2525
while (p !== null || q !== null) {
26-
const x: number = p !== null ? p.val : 0
27-
const y: number = q !== null ? q.val : 0
26+
const x: number = p === null ? 0 : p.val
27+
const y: number = q === null ? 0: q.val
2828
const sum: number = carry + x + y
2929
carry = Math.floor(sum / 10)
3030
curr.next = new ListNode(sum % 10)

src/main/ts/g0001_0100/s0003_longest_substring_without_repeating_characters/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function lengthOfLongestSubstring(s: string): number {
1010
let start: number = 0
1111
for (let i = 0; i < s.length; i++) {
1212
const cur: string = s.charAt(i)
13-
const charCode: number = cur.charCodeAt(0)
13+
const charCode: number = cur.codePointAt(0)!
1414
if (lastIndices[charCode] < start) {
1515
lastIndices[charCode] = i
1616
curLen++

src/main/ts/g0001_0100/s0008_string_to_integer_atoi/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
function myAtoi(s: string): number {
44
s = s.trim()
55
if (s.length === 0) return 0
6-
let result = parseInt(s) || 0
6+
let result = Number.parseInt(s) || 0
77
if (-1 * 2 ** 31 <= result && result <= 2 ** 31 - 1) {
88
return result
99
} else if (result < -1 * 2 ** 31) {

src/main/ts/g0001_0100/s0014_longest_common_prefix/solution.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ function longestCommonPrefix(strs: string[]): string {
1717
temp = temp.substring(0, strs[i].length)
1818
}
1919
cur = strs[i].substring(0, temp.length)
20-
if (cur !== temp) {
21-
temp = temp.substring(0, temp.length - 1)
22-
} else {
20+
if (cur === temp) {
2321
i++
22+
} else {
23+
temp = temp.substring(0, temp.length - 1)
2424
}
2525
}
2626
return temp

src/main/ts/g0001_0100/s0017_letter_combinations_of_a_phone_number/solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function findCombinations(start: number, nums: string, letters: string[], curr:
2020
return
2121
}
2222
for (let i = start; i < nums.length; i++) {
23-
const n = parseInt(nums.charAt(i))
23+
const n = Number.parseInt(nums.charAt(i))
2424
for (let j = 0; j < letters[n].length; j++) {
2525
const ch = letters[n].charAt(j)
2626
curr.push(ch)

src/main/ts/g0001_0100/s0027_remove_element/solution.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,15 @@
22
// #2025_03_31_Time_0_ms_(100.00%)_Space_56.09_MB_(20.94%)
33

44
function removeElement(nums: number[], val: number): number {
5-
if (!nums || nums.length === 0) {
6-
return 0
7-
}
8-
let len = nums.length
9-
let j = len - 1
10-
let occurTimes = 0
11-
for (let i = 0; i < len; i++) {
12-
if (nums[i] === val) {
13-
occurTimes++
14-
if (j === i) {
15-
return len - occurTimes
16-
}
17-
while (nums[j] === val) {
18-
j--
19-
occurTimes++
20-
if (j === i) {
21-
return len - occurTimes
22-
}
23-
}
24-
nums[i] = nums[j]
25-
j--
26-
}
27-
if (i === j) {
28-
return len - occurTimes
5+
let k = 0
6+
7+
for (const num of nums) {
8+
if (num !== val) {
9+
nums[k++] = num
2910
}
3011
}
31-
return len - occurTimes
12+
13+
return k
3214
}
3315

3416
export { removeElement }

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

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,80 @@
22
// #2025_04_01_Time_13_ms_(97.44%)_Space_63.70_MB_(46.03%)
33

44
function findSubstring(s: string, words: string[]): number[] {
5-
let ans: number[] = []
6-
let n1 = words[0].length
7-
let n2 = s.length
8-
let map1 = new Map<string, number>()
9-
for (let ch of words) {
10-
map1.set(ch, (map1.get(ch) ?? 0) + 1)
5+
if (words.length === 0) return []
6+
7+
const result: number[] = []
8+
const wordLen = words[0].length
9+
const totalLen = words.length * wordLen
10+
const limit = s.length
11+
12+
const target = buildFreqMap(words)
13+
14+
for (let offset = 0; offset < wordLen; offset++) {
15+
scanWindow(s, offset, wordLen, totalLen, limit, target, result)
1116
}
12-
for (let i = 0; i < n1; i++) {
13-
let left = i
14-
let j = i
15-
let c = 0
16-
let map2 = new Map<string, number>()
17-
while (j + n1 <= n2) {
18-
let word1 = s.substring(j, j + n1)
19-
j += n1
20-
if (map1.has(word1)) {
21-
map2.set(word1, (map2.get(word1) ?? 0) + 1)
22-
c++
23-
while ((map2.get(word1) ?? 0) > (map1.get(word1) ?? 0)) {
24-
let word2 = s.substring(left, left + n1)
25-
map2.set(word2, (map2.get(word2) ?? 0) - 1)
26-
left += n1
27-
c--
28-
}
29-
if (c === words.length) {
30-
ans.push(left)
31-
}
32-
} else {
33-
map2.clear()
34-
c = 0
35-
left = j
36-
}
17+
18+
return result
19+
}
20+
21+
function buildFreqMap(words: string[]): Map<string, number> {
22+
const map = new Map<string, number>()
23+
for (const w of words) {
24+
map.set(w, (map.get(w) ?? 0) + 1)
25+
}
26+
return map
27+
}
28+
29+
function scanWindow(
30+
s: string,
31+
start: number,
32+
wordLen: number,
33+
totalLen: number,
34+
limit: number,
35+
target: Map<string, number>,
36+
result: number[],
37+
): void {
38+
let left = start
39+
let count = 0
40+
const window = new Map<string, number>()
41+
42+
for (let right = start; right + wordLen <= limit; right += wordLen) {
43+
const word = s.substring(right, right + wordLen)
44+
45+
if (!target.has(word)) {
46+
window.clear()
47+
count = 0
48+
left = right + wordLen
49+
continue
3750
}
51+
52+
window.set(word, (window.get(word) ?? 0) + 1)
53+
count++
54+
55+
shrinkWindowIfNeeded(s, word, window, target, wordLen, () => {
56+
const removed = s.substring(left, left + wordLen)
57+
window.set(removed, (window.get(removed) ?? 0) - 1)
58+
left += wordLen
59+
count--
60+
})
61+
62+
if (count * wordLen === totalLen) {
63+
result.push(left)
64+
}
65+
}
66+
}
67+
68+
function shrinkWindowIfNeeded(
69+
_s: string,
70+
word: string,
71+
window: Map<string, number>,
72+
target: Map<string, number>,
73+
_wordLen: number,
74+
shrink: () => void,
75+
): void {
76+
while ((window.get(word) ?? 0) > (target.get(word) ?? 0)) {
77+
shrink()
3878
}
39-
return ans
4079
}
4180

4281
export { findSubstring }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function isValidSudoku(board: string[][]): boolean {
1010
if (board[i][j] === '.') {
1111
continue
1212
}
13-
let val = board[i][j].charCodeAt(0) - '0'.charCodeAt(0)
13+
let val = board[i][j].codePointAt(0)! - '0'.codePointAt(0)!
1414
let boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3)
1515
if (rowSet[i] & (1 << val) || colSet[j] & (1 << val) || boxSet[boxIndex] & (1 << val)) {
1616
return false

src/main/ts/g0001_0100/s0049_group_anagrams/solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ function groupAnagrams(strs: string[]): string[][] {
77
if (strs.length === 1) return [strs]
88
const map = new Map()
99
function getKey(str: string): string {
10-
const charCount = Array(26).fill(0)
10+
const charCount = new Array(26).fill(0)
1111
for (const char of str) {
12-
charCount[char.charCodeAt(0) - 'a'.charCodeAt(0)]++
12+
charCount[char.codePointAt(0)! - 'a'.codePointAt(0)!]++
1313
}
1414
return charCount.join('#')
1515
}

0 commit comments

Comments
 (0)