Skip to content

Commit d23b1f0

Browse files
committed
feat(leetcode): add 303 304 307
1 parent 4c7f884 commit d23b1f0

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default class NumArray {
2+
sumArray: number[];
3+
constructor(nums: number[]) {
4+
let n = nums.length;
5+
this.sumArray = new Array(n + 1).fill(0);
6+
for (let i = 0; i < n; i++) {
7+
this.sumArray[i + 1] = this.sumArray[i] + nums[i];
8+
}
9+
}
10+
11+
sumRange(i: number, j: number): number {
12+
return this.sumArray[j + 1] - this.sumArray[i];
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export default class NumMatrix {
2+
store?: number[][];
3+
constructor(matrix: number[][]) {
4+
let n = matrix.length;
5+
if (!n) return;
6+
let m = matrix[0].length;
7+
if (!m) return;
8+
this.store = new Array(n + 1);
9+
for (let i = 0; i < n + 1; i++) {
10+
this.store[i] = new Array(m + 1).fill(0);
11+
}
12+
13+
for (let i = 0; i < n; i++) {
14+
for (let j = 0; j < m; j++) {
15+
this.store[i + 1][j + 1] =
16+
matrix[i][j] +
17+
this.store[i][j + 1] +
18+
this.store[i + 1][j] -
19+
this.store[i][j];
20+
}
21+
}
22+
}
23+
24+
sumRegion(row1: number, col1: number, row2: number, col2: number): number {
25+
return this.store
26+
? this.store[row2 + 1][col2 + 1] -
27+
this.store[row1][col2 + 1] -
28+
this.store[row2 + 1][col1] +
29+
this.store[row1][col1]
30+
: 0;
31+
}
32+
}
33+
34+
/**
35+
* Your NumMatrix object will be instantiated and called as such:
36+
* var obj = new NumMatrix(matrix)
37+
* var param_1 = obj.sumRegion(row1,col1,row2,col2)
38+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default class NumArray {
2+
sumArray: number[];
3+
nums: number[];
4+
constructor(nums: number[]) {
5+
let n = nums.length;
6+
this.nums = nums;
7+
this.sumArray = new Array(n + 1).fill(0);
8+
for (let i = 0; i < n; i++) {
9+
this.sumArray[i + 1] = this.sumArray[i] + nums[i];
10+
}
11+
}
12+
13+
sumRange(i: number, j: number): number {
14+
return this.sumArray[j + 1] - this.sumArray[i];
15+
}
16+
17+
update(i: number, val: number): void {
18+
let sub = val - this.nums[i];
19+
this.nums[i] = val;
20+
for (i = i + 1; i < this.sumArray.length; i++) {
21+
this.sumArray[i] += sub;
22+
}
23+
}
24+
}
25+
26+
/**
27+
* Your NumArray object will be instantiated and called as such:
28+
* var obj = new NumArray(nums)
29+
* obj.update(i,val)
30+
* var param_2 = obj.sumRange(i,j)
31+
*/

src/playground/leetcode/342power-of-four/isPowerOfFour.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export default function isPowerOfFour(num: number): boolean {
1313
);
1414
/**
1515
return (
16-
(num > 0 && (num ^ 0x1) === 0) ||
16+
(num > 0 &&
17+
(num ^ 0x1) === 0) ||
1718
(num ^ 0x4) === 0 ||
1819
(num ^ 0x10) === 0 ||
1920
(num ^ 0x40) === 0 ||

0 commit comments

Comments
 (0)