Skip to content

Commit c77927e

Browse files
committed
feat: add solutions to lc problem: No.0304
No.0304.Range Sum Query 2D - Immutable
1 parent f36c397 commit c77927e

File tree

9 files changed

+226
-88
lines changed

9 files changed

+226
-88
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- [排序数组](/solution/0900-0999/0912.Sort%20an%20Array/README.md) - 快速排序、归并排序
4242
- [字符串相加](/solution/0400-0499/0415.Add%20Strings/README.md) - 高精度加法
4343
- [字符串相乘](/solution/0000-0099/0043.Multiply%20Strings/README.md) - 高精度乘法
44+
- [二维区域和检索 - 矩阵不可变](/solution/0300-0399/0304.Range%20Sum%20Query%202D%20-%20Immutable/README.md) - 二维前缀和
4445
<!-- 排序算法、待补充 -->
4546

4647
### 2. 搜索

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
4040
- [Sort an Array](/solution/0900-0999/0912.Sort%20an%20Array/README_EN.md) - Quick Sort, Merge Sort
4141
- [Add Strings](/solution/0400-0499/0415.Add%20Strings/README_EN.md) - Addition of large numbers
4242
- [Multiply Strings](/solution/0000-0099/0043.Multiply%20Strings/README_EN.md) - Multiply large numbers
43+
- [Range Sum Query 2D - Immutable](/solution/0300-0399/0304.Range%20Sum%20Query%202D%20-%20Immutable/README_EN.md) - Prefix sum
4344

4445
### 2. Search
4546

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

+78-27
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ numMatrix.sumRegion(1, 2, 2, 4); // return 12 (蓝色矩形框的元素总和)
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60-
动态规划-二维前缀和。
60+
**方法一:二维前缀和**
61+
62+
二维前缀和计算公式:`s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + nums[i][j]`
63+
64+
子矩阵和计算公式:`ans = s[x2 + 1][y2 + 1] - s[x2 + 1][y1] - s[x1][y2 + 1] + s[x1][y1]`
6165

6266
<!-- tabs:start -->
6367

@@ -70,13 +74,14 @@ class NumMatrix:
7074

7175
def __init__(self, matrix: List[List[int]]):
7276
m, n = len(matrix), len(matrix[0])
73-
self.pre = [[0] * (n + 1) for _ in range(m + 1)]
74-
for i in range(1, m + 1):
75-
for j in range(1, n + 1):
76-
self.pre[i][j] = self.pre[i - 1][j] + self.pre[i][j - 1] - self.pre[i - 1][j - 1] + matrix[i - 1][j - 1]
77+
self.s = [[0] * (n + 1) for _ in range(m + 1)]
78+
for i, row in enumerate(matrix):
79+
for j, v in enumerate(row):
80+
self.s[i + 1][j + 1] = self.s[i][j + 1] + self.s[i + 1][j] - self.s[i][j] + v
81+
7782

7883
def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
79-
return self.pre[row2 + 1][col2 + 1] - self.pre[row2 + 1][col1] - self.pre[row1][col2 + 1] + self.pre[row1][col1]
84+
return self.s[row2 + 1][col2 + 1] - self.s[row2 + 1][col1] - self.s[row1][col2 + 1] + self.s[row1][col1]
8085

8186

8287
# Your NumMatrix object will be instantiated and called as such:
@@ -90,20 +95,20 @@ class NumMatrix:
9095

9196
```java
9297
class NumMatrix {
93-
private int[][] pre;
98+
private int[][] s;
9499

95100
public NumMatrix(int[][] matrix) {
96101
int m = matrix.length, n = matrix[0].length;
97-
pre = new int[m + 1][n + 1];
98-
for (int i = 1; i <= m; ++i) {
99-
for (int j = 1; j <= n; ++j) {
100-
pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + matrix[i - 1][j - 1];
102+
s = new int[m + 1][n + 1];
103+
for (int i = 0; i < m; ++i) {
104+
for (int j = 0; j < n; ++j) {
105+
s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + matrix[i][j];
101106
}
102107
}
103108
}
104109

105110
public int sumRegion(int row1, int col1, int row2, int col2) {
106-
return pre[row2 + 1][col2 + 1] - pre[row2 + 1][col1] - pre[row1][col2 + 1] + pre[row1][col1];
111+
return s[row2 + 1][col2 + 1] - s[row2 + 1][col1] - s[row1][col2 + 1] + s[row1][col1];
107112
}
108113
}
109114

@@ -119,20 +124,22 @@ class NumMatrix {
119124
```cpp
120125
class NumMatrix {
121126
public:
122-
vector<vector<int>> pre;
127+
vector<vector<int>> s;
123128

124129
NumMatrix(vector<vector<int>>& matrix) {
125130
int m = matrix.size(), n = matrix[0].size();
126-
pre.resize(m + 1, vector<int>(n + 1));
127-
for (int i = 1; i <= m; ++i) {
128-
for (int j = 1; j <= n; ++j) {
129-
pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + matrix[i - 1][j - 1];
131+
s.resize(m + 1, vector<int>(n + 1));
132+
for (int i = 0; i < m; ++i)
133+
{
134+
for (int j = 0; j < n; ++j)
135+
{
136+
s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + matrix[i][j];
130137
}
131138
}
132139
}
133140

134141
int sumRegion(int row1, int col1, int row2, int col2) {
135-
return pre[row2 + 1][col2 + 1] - pre[row2 + 1][col1] - pre[row1][col2 + 1] + pre[row1][col1];
142+
return s[row2 + 1][col2 + 1] - s[row2 + 1][col1] - s[row1][col2 + 1] + s[row1][col1];
136143
}
137144
};
138145

@@ -147,25 +154,25 @@ public:
147154
148155
```go
149156
type NumMatrix struct {
150-
pre [][]int
157+
s [][]int
151158
}
152159
153160
func Constructor(matrix [][]int) NumMatrix {
154161
m, n := len(matrix), len(matrix[0])
155-
pre := make([][]int, m+1)
156-
for i := 0; i < m+1; i++ {
157-
pre[i] = make([]int, n+1)
162+
s := make([][]int, m+1)
163+
for i := range s {
164+
s[i] = make([]int, n+1)
158165
}
159-
for i := 1; i < m+1; i++ {
160-
for j := 1; j < n+1; j++ {
161-
pre[i][j] = pre[i-1][j] + pre[i][j-1] + -pre[i-1][j-1] + matrix[i-1][j-1]
166+
for i, row := range matrix {
167+
for j, v := range row {
168+
s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + v
162169
}
163170
}
164-
return NumMatrix{pre}
171+
return NumMatrix{s}
165172
}
166173
167174
func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
168-
return this.pre[row2+1][col2+1] - this.pre[row2+1][col1] - this.pre[row1][col2+1] + this.pre[row1][col1]
175+
return this.s[row2+1][col2+1] - this.s[row2+1][col1] - this.s[row1][col2+1] + this.s[row1][col1]
169176
}
170177
171178
/**
@@ -175,6 +182,50 @@ func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
175182
*/
176183
```
177184

185+
### **JavaScript**
186+
187+
```js
188+
/**
189+
* @param {number[][]} matrix
190+
*/
191+
var NumMatrix = function (matrix) {
192+
const m = matrix.length;
193+
const n = matrix[0].length;
194+
this.s = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0));
195+
for (let i = 0; i < m; ++i) {
196+
for (let j = 0; j < n; ++j) {
197+
this.s[i + 1][j + 1] =
198+
this.s[i + 1][j] +
199+
this.s[i][j + 1] -
200+
this.s[i][j] +
201+
matrix[i][j];
202+
}
203+
}
204+
};
205+
206+
/**
207+
* @param {number} row1
208+
* @param {number} col1
209+
* @param {number} row2
210+
* @param {number} col2
211+
* @return {number}
212+
*/
213+
NumMatrix.prototype.sumRegion = function (row1, col1, row2, col2) {
214+
return (
215+
this.s[row2 + 1][col2 + 1] -
216+
this.s[row2 + 1][col1] -
217+
this.s[row1][col2 + 1] +
218+
this.s[row1][col1]
219+
);
220+
};
221+
222+
/**
223+
* Your NumMatrix object will be instantiated and called as such:
224+
* var obj = new NumMatrix(matrix)
225+
* var param_1 = obj.sumRegion(row1,col1,row2,col2)
226+
*/
227+
```
228+
178229
### **...**
179230

180231
```

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

+75-28
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ class NumMatrix:
6161

6262
def __init__(self, matrix: List[List[int]]):
6363
m, n = len(matrix), len(matrix[0])
64-
self.pre = [[0] * (n + 1) for _ in range(m + 1)]
65-
for i in range(1, m + 1):
66-
for j in range(1, n + 1):
67-
self.pre[i][j] = self.pre[i - 1][j] + self.pre[i][j - 1] - self.pre[i - 1][j - 1] + matrix[i - 1][j - 1]
64+
self.s = [[0] * (n + 1) for _ in range(m + 1)]
65+
for i, row in enumerate(matrix):
66+
for j, v in enumerate(row):
67+
self.s[i + 1][j + 1] = self.s[i][j + 1] + self.s[i + 1][j] - self.s[i][j] + v
68+
6869

6970
def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
70-
return self.pre[row2 + 1][col2 + 1] - self.pre[row2 + 1][col1] - self.pre[row1][col2 + 1] + self.pre[row1][col1]
71+
return self.s[row2 + 1][col2 + 1] - self.s[row2 + 1][col1] - self.s[row1][col2 + 1] + self.s[row1][col1]
7172

7273

7374
# Your NumMatrix object will be instantiated and called as such:
@@ -79,20 +80,20 @@ class NumMatrix:
7980

8081
```java
8182
class NumMatrix {
82-
private int[][] pre;
83+
private int[][] s;
8384

8485
public NumMatrix(int[][] matrix) {
8586
int m = matrix.length, n = matrix[0].length;
86-
pre = new int[m + 1][n + 1];
87-
for (int i = 1; i <= m; ++i) {
88-
for (int j = 1; j <= n; ++j) {
89-
pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + matrix[i - 1][j - 1];
87+
s = new int[m + 1][n + 1];
88+
for (int i = 0; i < m; ++i) {
89+
for (int j = 0; j < n; ++j) {
90+
s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + matrix[i][j];
9091
}
9192
}
9293
}
9394

9495
public int sumRegion(int row1, int col1, int row2, int col2) {
95-
return pre[row2 + 1][col2 + 1] - pre[row2 + 1][col1] - pre[row1][col2 + 1] + pre[row1][col1];
96+
return s[row2 + 1][col2 + 1] - s[row2 + 1][col1] - s[row1][col2 + 1] + s[row1][col1];
9697
}
9798
}
9899

@@ -108,20 +109,22 @@ class NumMatrix {
108109
```cpp
109110
class NumMatrix {
110111
public:
111-
vector<vector<int>> pre;
112+
vector<vector<int>> s;
112113

113114
NumMatrix(vector<vector<int>>& matrix) {
114115
int m = matrix.size(), n = matrix[0].size();
115-
pre.resize(m + 1, vector<int>(n + 1));
116-
for (int i = 1; i <= m; ++i) {
117-
for (int j = 1; j <= n; ++j) {
118-
pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + matrix[i - 1][j - 1];
116+
s.resize(m + 1, vector<int>(n + 1));
117+
for (int i = 0; i < m; ++i)
118+
{
119+
for (int j = 0; j < n; ++j)
120+
{
121+
s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + matrix[i][j];
119122
}
120123
}
121124
}
122125

123126
int sumRegion(int row1, int col1, int row2, int col2) {
124-
return pre[row2 + 1][col2 + 1] - pre[row2 + 1][col1] - pre[row1][col2 + 1] + pre[row1][col1];
127+
return s[row2 + 1][col2 + 1] - s[row2 + 1][col1] - s[row1][col2 + 1] + s[row1][col1];
125128
}
126129
};
127130

@@ -132,29 +135,29 @@ public:
132135
*/
133136
```
134137
135-
### **C++**
138+
### **Go**
136139
137-
```cpp
140+
```go
138141
type NumMatrix struct {
139-
pre [][]int
142+
s [][]int
140143
}
141144
142145
func Constructor(matrix [][]int) NumMatrix {
143146
m, n := len(matrix), len(matrix[0])
144-
pre := make([][]int, m+1)
145-
for i := 0; i < m+1; i++ {
146-
pre[i] = make([]int, n+1)
147+
s := make([][]int, m+1)
148+
for i := range s {
149+
s[i] = make([]int, n+1)
147150
}
148-
for i := 1; i < m+1; i++ {
149-
for j := 1; j < n+1; j++ {
150-
pre[i][j] = pre[i-1][j] + pre[i][j-1] + -pre[i-1][j-1] + matrix[i-1][j-1]
151+
for i, row := range matrix {
152+
for j, v := range row {
153+
s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j] + v
151154
}
152155
}
153-
return NumMatrix{pre}
156+
return NumMatrix{s}
154157
}
155158
156159
func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
157-
return this.pre[row2+1][col2+1] - this.pre[row2+1][col1] - this.pre[row1][col2+1] + this.pre[row1][col1]
160+
return this.s[row2+1][col2+1] - this.s[row2+1][col1] - this.s[row1][col2+1] + this.s[row1][col1]
158161
}
159162
160163
/**
@@ -164,6 +167,50 @@ func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
164167
*/
165168
```
166169

170+
### **JavaScript**
171+
172+
```js
173+
/**
174+
* @param {number[][]} matrix
175+
*/
176+
var NumMatrix = function (matrix) {
177+
const m = matrix.length;
178+
const n = matrix[0].length;
179+
this.s = new Array(m + 1).fill(0).map(() => new Array(n + 1).fill(0));
180+
for (let i = 0; i < m; ++i) {
181+
for (let j = 0; j < n; ++j) {
182+
this.s[i + 1][j + 1] =
183+
this.s[i + 1][j] +
184+
this.s[i][j + 1] -
185+
this.s[i][j] +
186+
matrix[i][j];
187+
}
188+
}
189+
};
190+
191+
/**
192+
* @param {number} row1
193+
* @param {number} col1
194+
* @param {number} row2
195+
* @param {number} col2
196+
* @return {number}
197+
*/
198+
NumMatrix.prototype.sumRegion = function (row1, col1, row2, col2) {
199+
return (
200+
this.s[row2 + 1][col2 + 1] -
201+
this.s[row2 + 1][col1] -
202+
this.s[row1][col2 + 1] +
203+
this.s[row1][col1]
204+
);
205+
};
206+
207+
/**
208+
* Your NumMatrix object will be instantiated and called as such:
209+
* var obj = new NumMatrix(matrix)
210+
* var param_1 = obj.sumRegion(row1,col1,row2,col2)
211+
*/
212+
```
213+
167214
### **...**
168215

169216
```

solution/0300-0399/0304.Range Sum Query 2D - Immutable/Solution.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
class NumMatrix {
22
public:
3-
vector<vector<int>> pre;
3+
vector<vector<int>> s;
44

55
NumMatrix(vector<vector<int>>& matrix) {
66
int m = matrix.size(), n = matrix[0].size();
7-
pre.resize(m + 1, vector<int>(n + 1));
8-
for (int i = 1; i <= m; ++i) {
9-
for (int j = 1; j <= n; ++j) {
10-
pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + matrix[i - 1][j - 1];
7+
s.resize(m + 1, vector<int>(n + 1));
8+
for (int i = 0; i < m; ++i)
9+
{
10+
for (int j = 0; j < n; ++j)
11+
{
12+
s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + matrix[i][j];
1113
}
1214
}
1315
}
1416

1517
int sumRegion(int row1, int col1, int row2, int col2) {
16-
return pre[row2 + 1][col2 + 1] - pre[row2 + 1][col1] - pre[row1][col2 + 1] + pre[row1][col1];
18+
return s[row2 + 1][col2 + 1] - s[row2 + 1][col1] - s[row1][col2 + 1] + s[row1][col1];
1719
}
1820
};
1921

0 commit comments

Comments
 (0)