Skip to content

Commit ed54ca7

Browse files
committed
feat: update leetcode solutions: No.0304. Range Sum Query 2D - Immutable
1 parent 89cdf31 commit ed54ca7

File tree

4 files changed

+131
-46
lines changed

4 files changed

+131
-46
lines changed

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,58 @@ sumRegion(1, 2, 2, 4) -> 12
4545
<!-- 这里可写当前语言的特殊实现逻辑 -->
4646

4747
```python
48+
class NumMatrix:
4849

50+
def __init__(self, matrix: List[List[int]]):
51+
m = len(matrix)
52+
if m > 0:
53+
n = len(matrix[0])
54+
self.sums = [[0] * (n + 1) for _ in range(m + 1)]
55+
for i in range(m):
56+
for j in range(n):
57+
self.sums[i + 1][j + 1] = self.sums[i][j + 1] + \
58+
self.sums[i + 1][j] - self.sums[i][j] + matrix[i][j]
59+
60+
def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
61+
return self.sums[row2 + 1][col2 + 1] - self.sums[row2 + 1][col1] - self.sums[row1][col2 + 1] + self.sums[row1][col1]
62+
63+
64+
# Your NumMatrix object will be instantiated and called as such:
65+
# obj = NumMatrix(matrix)
66+
# param_1 = obj.sumRegion(row1,col1,row2,col2)
4967
```
5068

5169
### **Java**
5270

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

5573
```java
56-
74+
class NumMatrix {
75+
private int[][] sums;
76+
77+
public NumMatrix(int[][] matrix) {
78+
int m = matrix.length;
79+
if (m > 0) {
80+
int n = matrix[0].length;
81+
sums = new int[m + 1][n + 1];
82+
for (int i = 0; i < m; ++i) {
83+
for (int j = 0; j < n; ++j) {
84+
sums[i + 1][j + 1] = sums[i][j + 1] + sums[i + 1][j] - sums[i][j] + matrix[i][j];
85+
}
86+
}
87+
}
88+
}
89+
90+
public int sumRegion(int row1, int col1, int row2, int col2) {
91+
return sums[row2 + 1][col2 + 1] - sums[row2 + 1][col1] - sums[row1][col2 + 1] + sums[row1][col1];
92+
}
93+
}
94+
95+
/**
96+
* Your NumMatrix object will be instantiated and called as such:
97+
* NumMatrix obj = new NumMatrix(matrix);
98+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
99+
*/
57100
```
58101

59102
### **...**

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,56 @@ sumRegion(1, 2, 2, 4) -> 12
6565
### **Python3**
6666

6767
```python
68+
class NumMatrix:
6869

70+
def __init__(self, matrix: List[List[int]]):
71+
m = len(matrix)
72+
if m > 0:
73+
n = len(matrix[0])
74+
self.sums = [[0] * (n + 1) for _ in range(m + 1)]
75+
for i in range(m):
76+
for j in range(n):
77+
self.sums[i + 1][j + 1] = self.sums[i][j + 1] + \
78+
self.sums[i + 1][j] - self.sums[i][j] + matrix[i][j]
79+
80+
def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
81+
return self.sums[row2 + 1][col2 + 1] - self.sums[row2 + 1][col1] - self.sums[row1][col2 + 1] + self.sums[row1][col1]
82+
83+
84+
# Your NumMatrix object will be instantiated and called as such:
85+
# obj = NumMatrix(matrix)
86+
# param_1 = obj.sumRegion(row1,col1,row2,col2)
6987
```
7088

7189
### **Java**
7290

7391
```java
74-
92+
class NumMatrix {
93+
private int[][] sums;
94+
95+
public NumMatrix(int[][] matrix) {
96+
int m = matrix.length;
97+
if (m > 0) {
98+
int n = matrix[0].length;
99+
sums = new int[m + 1][n + 1];
100+
for (int i = 0; i < m; ++i) {
101+
for (int j = 0; j < n; ++j) {
102+
sums[i + 1][j + 1] = sums[i][j + 1] + sums[i + 1][j] - sums[i][j] + matrix[i][j];
103+
}
104+
}
105+
}
106+
}
107+
108+
public int sumRegion(int row1, int col1, int row2, int col2) {
109+
return sums[row2 + 1][col2 + 1] - sums[row2 + 1][col1] - sums[row1][col2 + 1] + sums[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+
*/
75118
```
76119

77120
### **...**
Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,26 @@
1-
public class NumMatrix {
2-
3-
public long[][] sumMatrix;
4-
5-
public NumMatrix(int[][] matrix) {
6-
7-
if (matrix == null || matrix.length == 0) {
8-
return;
9-
}
10-
11-
sumMatrix = new long[matrix.length + 1][matrix[0].length + 1];
12-
13-
for (int i = 0; i < matrix.length; i++) {
14-
for (int j = 0; j < matrix[0].length; j++) {
15-
sumMatrix[i][j + 1] = sumMatrix[i][j] + matrix[i][j];
16-
}
17-
}
18-
}
19-
20-
public int sumRegion(int row1, int col1, int row2, int col2) {
21-
22-
if (sumMatrix == null || row1 < 0 || row2 < 0 || col1 < 0
23-
|| col2 < 0 || row1 >= sumMatrix.length - 1
24-
|| row2 >= sumMatrix.length - 1
25-
|| col1 >= sumMatrix[0].length - 1
26-
|| col2 >= sumMatrix[0].length - 1 || row1 > row2
27-
|| col1 > col2) {
28-
return Integer.MIN_VALUE;
29-
}
30-
31-
long rt = 0;
32-
33-
for (int i = row1; i <= row2; i++) {
34-
rt += sumMatrix[i][col2 + 1] - sumMatrix[i][col1];
35-
}
36-
37-
return (int) rt;
38-
}
39-
1+
class NumMatrix {
2+
private int[][] sums;
3+
4+
public NumMatrix(int[][] matrix) {
5+
int m = matrix.length;
6+
if (m > 0) {
7+
int n = matrix[0].length;
8+
sums = new int[m + 1][n + 1];
9+
for (int i = 0; i < m; ++i) {
10+
for (int j = 0; j < n; ++j) {
11+
sums[i + 1][j + 1] = sums[i][j + 1] + sums[i + 1][j] - sums[i][j] + matrix[i][j];
12+
}
13+
}
14+
}
15+
}
16+
17+
public int sumRegion(int row1, int col1, int row2, int col2) {
18+
return sums[row2 + 1][col2 + 1] - sums[row2 + 1][col1] - sums[row1][col2 + 1] + sums[row1][col1];
19+
}
4020
}
4121

42-
43-
// Your NumMatrix object will be instantiated and called as such:
44-
// NumMatrix numMatrix = new NumMatrix(matrix);
45-
// numMatrix.sumRegion(0, 1, 2, 3);
46-
// numMatrix.sumRegion(1, 2, 3, 4);
22+
/**
23+
* Your NumMatrix object will be instantiated and called as such:
24+
* NumMatrix obj = new NumMatrix(matrix);
25+
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
26+
*/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class NumMatrix:
2+
3+
def __init__(self, matrix: List[List[int]]):
4+
m = len(matrix)
5+
if m > 0:
6+
n = len(matrix[0])
7+
self.sums = [[0] * (n + 1) for _ in range(m + 1)]
8+
for i in range(m):
9+
for j in range(n):
10+
self.sums[i + 1][j + 1] = self.sums[i][j + 1] + \
11+
self.sums[i + 1][j] - self.sums[i][j] + matrix[i][j]
12+
13+
def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
14+
return self.sums[row2 + 1][col2 + 1] - self.sums[row2 + 1][col1] - self.sums[row1][col2 + 1] + self.sums[row1][col1]
15+
16+
17+
# Your NumMatrix object will be instantiated and called as such:
18+
# obj = NumMatrix(matrix)
19+
# param_1 = obj.sumRegion(row1,col1,row2,col2)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy