Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1d34870

Browse files
feat: add solutions to lcof2 problem: No.013
1 parent fbf1f14 commit 1d34870

File tree

5 files changed

+192
-2
lines changed

5 files changed

+192
-2
lines changed

‎lcof2/剑指 Offer II 013. 二维子矩阵的和/README.md‎

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
<p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcof2/%E5%89%91%E6%8C%87%20Offer%20II%20013.%20%E4%BA%8C%E7%BB%B4%E5%AD%90%E7%9F%A9%E9%98%B5%E7%9A%84%E5%92%8C/images/1626332422-wUpUHT-image.png" style="width: 200px;" /></p>
2525

2626
<pre>
27-
<strong>输入:</strong>
27+
<strong>输入:</strong>
2828
[&quot;NumMatrix&quot;,&quot;sumRegion&quot;,&quot;sumRegion&quot;,&quot;sumRegion&quot;]
2929
[[[[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]],[2,1,4,3],[1,1,2,2],[1,2,2,4]]
30-
<strong>输出:</strong>
30+
<strong>输出:</strong>
3131
[null, 8, 11, 12]
3232

3333
<strong>解释:</strong>
@@ -60,22 +60,122 @@ numMatrix.sumRegion(1, 2, 2, 4); // return 12 (蓝色矩形框的元素总和)
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
动态规划-二维前缀和。
64+
6365
<!-- tabs:start -->
6466

6567
### **Python3**
6668

6769
<!-- 这里可写当前语言的特殊实现逻辑 -->
6870

6971
```python
72+
class NumMatrix:
73+
74+
def __init__(self, matrix: List[List[int]]):
75+
m, n = len(matrix), len(matrix[0])
76+
self.pre = [[0] * (n + 1) for _ in range(m + 1)]
77+
for i in range(1, m + 1):
78+
for j in range(1, n + 1):
79+
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]
80+
81+
def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
82+
return self.pre[row2 + 1][col2 + 1] - self.pre[row2 + 1][col1] - self.pre[row1][col2 + 1] + self.pre[row1][col1]
83+
7084

85+
# Your NumMatrix object will be instantiated and called as such:
86+
# obj = NumMatrix(matrix)
87+
# param_1 = obj.sumRegion(row1,col1,row2,col2)
7188
```
7289

7390
### **Java**
7491

7592
<!-- 这里可写当前语言的特殊实现逻辑 -->
7693

7794
```java
95+
class NumMatrix {
96+
private int[][] pre;
97+
98+
public NumMatrix(int[][] matrix) {
99+
int m = matrix.length, n = matrix[0].length;
100+
pre = new int[m + 1][n + 1];
101+
for (int i = 1; i <= m; ++i) {
102+
for (int j = 1; j <= n; ++j) {
103+
pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + matrix[i - 1][j - 1];
104+
}
105+
}
106+
}
107+
108+
public int sumRegion(int row1, int col1, int row2, int col2) {
109+
return pre[row2 + 1][col2 + 1] - pre[row2 + 1][col1] - pre[row1][col2 + 1] + pre[row1][col1];
110+
}
111+
}
112+
113+
/**
114+
* Your NumMatrix object will be instantiated and called as such:
115+
* NumMatrix obj = new NumMatrix(matrix);
116+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
117+
*/
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class NumMatrix {
124+
public:
125+
vector<vector<int>> pre;
126+
127+
NumMatrix(vector<vector<int>>& matrix) {
128+
int m = matrix.size(), n = matrix[0].size();
129+
pre.resize(m + 1, vector<int>(n + 1));
130+
for (int i = 1; i <= m; ++i) {
131+
for (int j = 1; j <= n; ++j) {
132+
pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + matrix[i - 1][j - 1];
133+
}
134+
}
135+
}
136+
137+
int sumRegion(int row1, int col1, int row2, int col2) {
138+
return pre[row2 + 1][col2 + 1] - pre[row2 + 1][col1] - pre[row1][col2 + 1] + pre[row1][col1];
139+
}
140+
};
141+
142+
/**
143+
* Your NumMatrix object will be instantiated and called as such:
144+
* NumMatrix* obj = new NumMatrix(matrix);
145+
* int param_1 = obj->sumRegion(row1,col1,row2,col2);
146+
*/
147+
```
78148
149+
### **Go**
150+
151+
```go
152+
type NumMatrix struct {
153+
pre [][]int
154+
}
155+
156+
func Constructor(matrix [][]int) NumMatrix {
157+
m, n := len(matrix), len(matrix[0])
158+
pre := make([][]int, m+1)
159+
for i := 0; i < m+1; i++ {
160+
pre[i] = make([]int, n+1)
161+
}
162+
for i := 1; i < m+1; i++ {
163+
for j := 1; j < n+1; j++ {
164+
pre[i][j] = pre[i-1][j] + pre[i][j-1] + -pre[i-1][j-1] + matrix[i-1][j-1]
165+
}
166+
}
167+
return NumMatrix{pre}
168+
}
169+
170+
func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
171+
return this.pre[row2+1][col2+1] - this.pre[row2+1][col1] - this.pre[row1][col2+1] + this.pre[row1][col1]
172+
}
173+
174+
/**
175+
* Your NumMatrix object will be instantiated and called as such:
176+
* obj := Constructor(matrix);
177+
* param_1 := obj.SumRegion(row1,col1,row2,col2);
178+
*/
79179
```
80180

81181
### **...**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class NumMatrix {
2+
public:
3+
vector<vector<int>> pre;
4+
5+
NumMatrix(vector<vector<int>>& matrix) {
6+
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];
11+
}
12+
}
13+
}
14+
15+
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];
17+
}
18+
};
19+
20+
/**
21+
* Your NumMatrix object will be instantiated and called as such:
22+
* NumMatrix* obj = new NumMatrix(matrix);
23+
* int param_1 = obj->sumRegion(row1,col1,row2,col2);
24+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
type NumMatrix struct {
2+
pre [][]int
3+
}
4+
5+
func Constructor(matrix [][]int) NumMatrix {
6+
m, n := len(matrix), len(matrix[0])
7+
pre := make([][]int, m+1)
8+
for i := 0; i < m+1; i++ {
9+
pre[i] = make([]int, n+1)
10+
}
11+
for i := 1; i < m+1; i++ {
12+
for j := 1; j < n+1; j++ {
13+
pre[i][j] = pre[i-1][j] + pre[i][j-1] + -pre[i-1][j-1] + matrix[i-1][j-1]
14+
}
15+
}
16+
return NumMatrix{pre}
17+
}
18+
19+
func (this *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
20+
return this.pre[row2+1][col2+1] - this.pre[row2+1][col1] - this.pre[row1][col2+1] + this.pre[row1][col1]
21+
}
22+
23+
/**
24+
* Your NumMatrix object will be instantiated and called as such:
25+
* obj := Constructor(matrix);
26+
* param_1 := obj.SumRegion(row1,col1,row2,col2);
27+
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class NumMatrix {
2+
private int[][] pre;
3+
4+
public NumMatrix(int[][] matrix) {
5+
int m = matrix.length, n = matrix[0].length;
6+
pre = new int[m + 1][n + 1];
7+
for (int i = 1; i <= m; ++i) {
8+
for (int j = 1; j <= n; ++j) {
9+
pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + matrix[i - 1][j - 1];
10+
}
11+
}
12+
}
13+
14+
public int sumRegion(int row1, int col1, int row2, int col2) {
15+
return pre[row2 + 1][col2 + 1] - pre[row2 + 1][col1] - pre[row1][col2 + 1] + pre[row1][col1];
16+
}
17+
}
18+
19+
/**
20+
* Your NumMatrix object will be instantiated and called as such:
21+
* NumMatrix obj = new NumMatrix(matrix);
22+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
23+
*/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class NumMatrix:
2+
3+
def __init__(self, matrix: List[List[int]]):
4+
m, n = len(matrix), len(matrix[0])
5+
self.pre = [[0] * (n + 1) for _ in range(m + 1)]
6+
for i in range(1, m + 1):
7+
for j in range(1, n + 1):
8+
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]
9+
10+
def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
11+
return self.pre[row2 + 1][col2 + 1] - self.pre[row2 + 1][col1] - self.pre[row1][col2 + 1] + self.pre[row1][col1]
12+
13+
14+
# Your NumMatrix object will be instantiated and called as such:
15+
# obj = NumMatrix(matrix)
16+
# param_1 = obj.sumRegion(row1,col1,row2,col2)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /