Skip to content

Commit f66105e

Browse files
committed
update some types error
1 parent 0bf9eb8 commit f66105e

9 files changed

+166
-69
lines changed

src/ListNode.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class ListNode {
2-
next: ListNode;
32
val: number;
4-
constructor(val: number) {
5-
this.val = val;
3+
next: ListNode | null = null;
4+
constructor(val?: number) {
5+
this.val = val === undefined ? 0 : val;
66
}
77
}

src/arithmetic-slices.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function numberOfArithmeticSlices(nums: number[]): number {
2+
const N = nums.length;
3+
const dp = Array.from({ length: N }, () => 0);
4+
for (let i = 1; i < N - 1; i++) {
5+
if (nums[i] - nums[i - 1] === nums[i + 1] - nums[i]) {
6+
dp[i] = dp[i - 1] + 1;
7+
}
8+
}
9+
return dp.reduce((prev, next) => prev + next, 0);
10+
}
11+
12+
/**
13+
// space optimize
14+
function numberOfArithmeticSlices(nums: number[]): number {
15+
const N = nums.length;
16+
17+
let acc = 0;
18+
let ret = 0;
19+
for (let i = 1; i < N - 1; i++) {
20+
let current = 0;
21+
if (nums[i] - nums[i - 1] === nums[i + 1] - nums[i]) {
22+
acc++;
23+
} else {
24+
acc = 0;
25+
}
26+
ret += acc;
27+
}
28+
return ret;
29+
}
30+
*/

src/middle-of-the-linked-list.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function middleNode(head: ListNode | null): ListNode | null {
2+
if (head === null) return null;
3+
4+
let slow = head,
5+
fast: ListNode | null = head;
6+
7+
while (fast !== null && fast.next !== null) {
8+
slow = slow.next!;
9+
fast = fast.next.next;
10+
}
11+
return slow;
12+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
2+
const dummy = new ListNode();
3+
dummy.next = head;
4+
5+
let slow: ListNode | null = dummy,
6+
fast: ListNode | null = dummy;
7+
8+
while (n-- !== 0 && fast != null) fast = fast.next; // revisit later
9+
if (fast === null) return head; // means n > list node counts
10+
11+
while (fast.next !== null) {
12+
fast = fast.next;
13+
slow = slow!.next;
14+
}
15+
16+
// remove slow.next
17+
slow!.next = slow!.next!.next;
18+
19+
return dummy.next;
20+
}

src/reverse-node-kth-group.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/reverse-nodes-in-k-group.ts

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,21 @@
1-
/**
2-
* Definition for singly-linked list.
3-
* class ListNode {
4-
* val: number
5-
* next: ListNode | null
6-
* constructor(val?: number, next?: ListNode | null) {
7-
* this.val = (val===undefined ? 0 : val)
8-
* this.next = (next===undefined ? null : next)
9-
* }
10-
* }
11-
*/
12-
13-
const reverse = (head: ListNode, tail: ListNode): void => {
14-
const tailNext = tail.next;
15-
let prev = tailNext;
16-
while (head !== tailNext) {
17-
const next = head.next;
18-
head.next = prev;
19-
prev = head;
20-
head = next;
21-
}
22-
};
231
function reverseKGroup(head: ListNode | null, k: number): ListNode | null {
24-
let f = head;
25-
for (let i = 1; i < k && f !== null; i++) f = f.next;
26-
if (f === null) return head;
27-
f.next = reverseKGroup(f.next, k);
28-
reverse(head, f);
29-
return f;
2+
if (head === null) return null;
3+
4+
const reverse = (head: ListNode, tail: ListNode): void => {
5+
const tailNext = tail.next;
6+
let prev = tailNext;
7+
while (head !== tailNext) {
8+
const next = head.next;
9+
head.next = prev;
10+
prev = head;
11+
head = next!;
12+
}
13+
};
14+
15+
let f: ListNode | null = head;
16+
for (let i = 1; i < k && f !== null; i++) f = f.next;
17+
if (f === null) return head;
18+
f.next = reverseKGroup(f.next, k);
19+
reverse(head, f);
20+
return f;
3021
}

src/search-a-2d-matrix.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export {};
2+
3+
const findIndex = (
4+
arr: number[],
5+
compareFn: (val: number) => number
6+
): number => {
7+
// binary search with compareFn
8+
// O(log n)
9+
10+
const N = arr.length;
11+
let lo = 0,
12+
hi = N - 1;
13+
14+
while (lo <= hi) {
15+
const mid = Math.floor((lo + hi) / 2);
16+
const val = arr[mid];
17+
18+
if (compareFn(val) === 0) return mid;
19+
20+
if (compareFn(val) > 0) hi = mid - 1;
21+
else lo = mid + 1;
22+
}
23+
24+
return lo - 1;
25+
};
26+
function searchMatrix(matrix: number[][], target: number): boolean {
27+
// search the row start to find the close small or equal value's index check which row
28+
29+
const rowIdx = findIndex(
30+
matrix.map((row) => row[0]),
31+
(val) => (val > target ? 1 : -1)
32+
);
33+
if (rowIdx === -1) return false;
34+
35+
const colIdx = findIndex(matrix[rowIdx], (val) => val - target);
36+
37+
return matrix[rowIdx][colIdx] === target;
38+
// search the specific row
39+
}

src/valid-sudoku.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function isValidSudoku(board: string[][]): boolean {
2+
const N = board.length;
3+
4+
const valid = (i: number, j: number): boolean => {
5+
const rowSet: Set<string> = new Set();
6+
// row validate
7+
for (let k = 0; k < N; k++) {
8+
if (board[i][k] === ".") continue;
9+
if (rowSet.has(board[i][k])) return false;
10+
rowSet.add(board[i][k]);
11+
}
12+
13+
const colSet: Set<string> = new Set();
14+
// col validate
15+
for (let k = 0; k < N; k++) {
16+
if (board[k][j] === ".") continue;
17+
if (colSet.has(board[k][j])) return false;
18+
colSet.add(board[k][j]);
19+
}
20+
21+
const boxSet: Set<string> = new Set();
22+
// box validate
23+
const istart = Math.floor(i / 3) * 3;
24+
const jstart = Math.floor(j / 3) * 3;
25+
for (let ii = istart; ii < istart + 3; ii++) {
26+
for (let jj = jstart; jj < jstart + 3; jj++) {
27+
if (board[ii][jj] === ".") continue;
28+
if (boxSet.has(board[ii][jj])) return false;
29+
boxSet.add(board[ii][jj]);
30+
}
31+
}
32+
return true;
33+
};
34+
35+
for (let i = 0; i < N; i++) {
36+
for (let j = 0; j < N; j++) {
37+
if (board[i][j] === ".") {
38+
if (valid(i, j) === false) return false;
39+
}
40+
}
41+
}
42+
return true;
43+
}

0 commit comments

Comments
 (0)