Skip to content

Commit 05e8a44

Browse files
committed
feat: add solutions to lc problems: No.0303,0304
* No.0303.Range Sum Query - Immutable * No.0370.Range Addition
1 parent 9421b98 commit 05e8a44

File tree

14 files changed

+311
-110
lines changed

14 files changed

+311
-110
lines changed

solution/0300-0399/0303.Range Sum Query - Immutable/README.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ numArray.sumRange(0, 5); // return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1))
5656

5757
前缀和计算公式:`s[i + 1] = s[i] + nums[i]`
5858

59+
初始化的时间复杂度是 $O(n)$,每次查询的时间复杂度是 $O(1)$。其中 $n$ 是数组的长度。
60+
5961
<!-- tabs:start -->
6062

6163
### **Python3**
@@ -65,7 +67,7 @@ numArray.sumRange(0, 5); // return -3 ((-2) + 0 + 3 + (-5) + 2 + (-1))
6567
```python
6668
class NumArray:
6769
def __init__(self, nums: List[int]):
68-
self.s = [0] + list(accumulate(nums))
70+
self.s = list(accumulate(nums, initial=0))
6971

7072
def sumRange(self, left: int, right: int) -> int:
7173
return self.s[right + 1] - self.s[left]
@@ -156,6 +158,32 @@ func (this *NumArray) SumRange(left int, right int) int {
156158
*/
157159
```
158160

161+
### **TypeScript**
162+
163+
```ts
164+
class NumArray {
165+
private s: number[];
166+
167+
constructor(nums: number[]) {
168+
const n = nums.length;
169+
this.s = new Array(n + 1).fill(0);
170+
for (let i = 0; i < n; ++i) {
171+
this.s[i + 1] = this.s[i] + nums[i];
172+
}
173+
}
174+
175+
sumRange(left: number, right: number): number {
176+
return this.s[right + 1] - this.s[left];
177+
}
178+
}
179+
180+
/**
181+
* Your NumArray object will be instantiated and called as such:
182+
* var obj = new NumArray(nums)
183+
* var param_1 = obj.sumRange(left,right)
184+
*/
185+
```
186+
159187
### **Rust**
160188

161189
```rust

solution/0300-0399/0303.Range Sum Query - Immutable/README_EN.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
5353
```python
5454
class NumArray:
5555
def __init__(self, nums: List[int]):
56-
self.s = [0] + list(accumulate(nums))
56+
self.s = list(accumulate(nums, initial=0))
5757

5858
def sumRange(self, left: int, right: int) -> int:
5959
return self.s[right + 1] - self.s[left]
@@ -142,6 +142,32 @@ func (this *NumArray) SumRange(left int, right int) int {
142142
*/
143143
```
144144

145+
### **TypeScript**
146+
147+
```ts
148+
class NumArray {
149+
private s: number[];
150+
151+
constructor(nums: number[]) {
152+
const n = nums.length;
153+
this.s = new Array(n + 1).fill(0);
154+
for (let i = 0; i < n; ++i) {
155+
this.s[i + 1] = this.s[i] + nums[i];
156+
}
157+
}
158+
159+
sumRange(left: number, right: number): number {
160+
return this.s[right + 1] - this.s[left];
161+
}
162+
}
163+
164+
/**
165+
* Your NumArray object will be instantiated and called as such:
166+
* var obj = new NumArray(nums)
167+
* var param_1 = obj.sumRange(left,right)
168+
*/
169+
```
170+
145171
### **Rust**
146172

147173
```rust

solution/0300-0399/0303.Range Sum Query - Immutable/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class NumArray:
22
def __init__(self, nums: List[int]):
3-
self.s = [0] + list(accumulate(nums))
3+
self.s = list(accumulate(nums, initial=0))
44

55
def sumRange(self, left: int, right: int) -> int:
66
return self.s[right + 1] - self.s[left]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class NumArray {
2+
private s: number[];
3+
4+
constructor(nums: number[]) {
5+
const n = nums.length;
6+
this.s = new Array(n + 1).fill(0);
7+
for (let i = 0; i < n; ++i) {
8+
this.s[i + 1] = this.s[i] + nums[i];
9+
}
10+
}
11+
12+
sumRange(left: number, right: number): number {
13+
return this.s[right + 1] - this.s[left];
14+
}
15+
}
16+
17+
/**
18+
* Your NumArray object will be instantiated and called as such:
19+
* var obj = new NumArray(nums)
20+
* var param_1 = obj.sumRange(left,right)
21+
*/

solution/0300-0399/0304.Range Sum Query 2D - Immutable/README.md

+51-3
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,21 @@ numMatrix.sumRegion(1, 2, 2, 4); // return 12 (蓝色矩形框的元素总和)
5959

6060
**方法一:二维前缀和**
6161

62-
`s[i + 1][j + 1]` 表示第 i 行第 j 列左上部分所有元素之和,其中 i, j 下标从 0 开始。
62+
我们用 $s[i + 1][j + 1]$ 表示第 $i$ 行第 $j$ 列左上部分所有元素之和,下标 $i$ 和 $j$ 均从 $0$ 开始。可以得到以下前缀和公式:
6363

64-
`s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + nums[i][j]`
64+
$$
65+
s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + nums[i][j]
66+
$$
6567

66-
以 (x1, y1) 为左上角,(x2, y2) 为右下角的子矩阵和 `sub = s[x2 + 1][y2 + 1] - s[x2 + 1][y1] - s[x1][y2 + 1] + s[x1][y1]`
68+
那么分别以 $(x_1, y_1)$ 和 $(x_2, y_2)$ 为左上角和右下角的矩形的元素之和为:
69+
70+
$$
71+
s[x_2 + 1][y_2 + 1] - s[x_2 + 1][y_1] - s[x_1][y_2 + 1] + s[x_1][y_1]
72+
$$
73+
74+
我们在初始化方法中预处理出前缀和数组 $s$,在查询方法中直接返回上述公式的结果即可。
75+
76+
初始化的时间复杂度为 $O(m\times n)$,查询的时间复杂度为 $O(1)$。
6777

6878
<!-- tabs:start -->
6979

@@ -231,6 +241,44 @@ NumMatrix.prototype.sumRegion = function (row1, col1, row2, col2) {
231241
*/
232242
```
233243

244+
### **TypeScript**
245+
246+
```ts
247+
class NumMatrix {
248+
private s: number[][];
249+
250+
constructor(matrix: number[][]) {
251+
const m = matrix.length;
252+
const n = matrix[0].length;
253+
this.s = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0));
254+
for (let i = 0; i < m; ++i) {
255+
for (let j = 0; j < n; ++j) {
256+
this.s[i + 1][j + 1] =
257+
this.s[i + 1][j] +
258+
this.s[i][j + 1] -
259+
this.s[i][j] +
260+
matrix[i][j];
261+
}
262+
}
263+
}
264+
265+
sumRegion(row1: number, col1: number, row2: number, col2: number): number {
266+
return (
267+
this.s[row2 + 1][col2 + 1] -
268+
this.s[row2 + 1][col1] -
269+
this.s[row1][col2 + 1] +
270+
this.s[row1][col1]
271+
);
272+
}
273+
}
274+
275+
/**
276+
* Your NumMatrix object will be instantiated and called as such:
277+
* var obj = new NumMatrix(matrix)
278+
* var param_1 = obj.sumRegion(row1,col1,row2,col2)
279+
*/
280+
```
281+
234282
### **...**
235283

236284
```

solution/0300-0399/0304.Range Sum Query 2D - Immutable/README_EN.md

+38
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,44 @@ NumMatrix.prototype.sumRegion = function (row1, col1, row2, col2) {
216216
*/
217217
```
218218

219+
### **TypeScript**
220+
221+
```ts
222+
class NumMatrix {
223+
private s: number[][];
224+
225+
constructor(matrix: number[][]) {
226+
const m = matrix.length;
227+
const n = matrix[0].length;
228+
this.s = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0));
229+
for (let i = 0; i < m; ++i) {
230+
for (let j = 0; j < n; ++j) {
231+
this.s[i + 1][j + 1] =
232+
this.s[i + 1][j] +
233+
this.s[i][j + 1] -
234+
this.s[i][j] +
235+
matrix[i][j];
236+
}
237+
}
238+
}
239+
240+
sumRegion(row1: number, col1: number, row2: number, col2: number): number {
241+
return (
242+
this.s[row2 + 1][col2 + 1] -
243+
this.s[row2 + 1][col1] -
244+
this.s[row1][col2 + 1] +
245+
this.s[row1][col1]
246+
);
247+
}
248+
}
249+
250+
/**
251+
* Your NumMatrix object will be instantiated and called as such:
252+
* var obj = new NumMatrix(matrix)
253+
* var param_1 = obj.sumRegion(row1,col1,row2,col2)
254+
*/
255+
```
256+
219257
### **...**
220258

221259
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class NumMatrix {
2+
private s: number[][];
3+
4+
constructor(matrix: number[][]) {
5+
const m = matrix.length;
6+
const n = matrix[0].length;
7+
this.s = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0));
8+
for (let i = 0; i < m; ++i) {
9+
for (let j = 0; j < n; ++j) {
10+
this.s[i + 1][j + 1] =
11+
this.s[i + 1][j] +
12+
this.s[i][j + 1] -
13+
this.s[i][j] +
14+
matrix[i][j];
15+
}
16+
}
17+
}
18+
19+
sumRegion(row1: number, col1: number, row2: number, col2: number): number {
20+
return (
21+
this.s[row2 + 1][col2 + 1] -
22+
this.s[row2 + 1][col1] -
23+
this.s[row1][col2 + 1] +
24+
this.s[row1][col1]
25+
);
26+
}
27+
}
28+
29+
/**
30+
* Your NumMatrix object will be instantiated and called as such:
31+
* var obj = new NumMatrix(matrix)
32+
* var param_1 = obj.sumRegion(row1,col1,row2,col2)
33+
*/

0 commit comments

Comments
 (0)