Skip to content

Commit

Permalink
feat: update leetcode solutions: No.0304. Range Sum Query 2D - Immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Apr 4, 2021
1 parent 89cdf31 commit ed54ca7
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 46 deletions.
45 changes: 44 additions & 1 deletion solution/0300-0399/0304.Range Sum Query 2D - Immutable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,58 @@ sumRegion(1, 2, 2, 4) -> 12
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python
class NumMatrix:

def __init__(self, matrix: List[List[int]]):
m = len(matrix)
if m > 0:
n = len(matrix[0])
self.sums = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m):
for j in range(n):
self.sums[i + 1][j + 1] = self.sums[i][j + 1] + \
self.sums[i + 1][j] - self.sums[i][j] + matrix[i][j]

def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
return self.sums[row2 + 1][col2 + 1] - self.sums[row2 + 1][col1] - self.sums[row1][col2 + 1] + self.sums[row1][col1]


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

### **Java**

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

```java

class NumMatrix {
private int[][] sums;

public NumMatrix(int[][] matrix) {
int m = matrix.length;
if (m > 0) {
int n = matrix[0].length;
sums = new int[m + 1][n + 1];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
sums[i + 1][j + 1] = sums[i][j + 1] + sums[i + 1][j] - sums[i][j] + matrix[i][j];
}
}
}
}

public int sumRegion(int row1, int col1, int row2, int col2) {
return sums[row2 + 1][col2 + 1] - sums[row2 + 1][col1] - sums[row1][col2 + 1] + sums[row1][col1];
}
}

/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,56 @@ sumRegion(1, 2, 2, 4) -> 12
### **Python3**

```python
class NumMatrix:

def __init__(self, matrix: List[List[int]]):
m = len(matrix)
if m > 0:
n = len(matrix[0])
self.sums = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m):
for j in range(n):
self.sums[i + 1][j + 1] = self.sums[i][j + 1] + \
self.sums[i + 1][j] - self.sums[i][j] + matrix[i][j]

def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
return self.sums[row2 + 1][col2 + 1] - self.sums[row2 + 1][col1] - self.sums[row1][col2 + 1] + self.sums[row1][col1]


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

### **Java**

```java

class NumMatrix {
private int[][] sums;

public NumMatrix(int[][] matrix) {
int m = matrix.length;
if (m > 0) {
int n = matrix[0].length;
sums = new int[m + 1][n + 1];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
sums[i + 1][j + 1] = sums[i][j + 1] + sums[i + 1][j] - sums[i][j] + matrix[i][j];
}
}
}
}

public int sumRegion(int row1, int col1, int row2, int col2) {
return sums[row2 + 1][col2 + 1] - sums[row2 + 1][col1] - sums[row1][col2 + 1] + sums[row1][col1];
}
}

/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,26 @@
public class NumMatrix {

public long[][] sumMatrix;

public NumMatrix(int[][] matrix) {

if (matrix == null || matrix.length == 0) {
return;
}

sumMatrix = new long[matrix.length + 1][matrix[0].length + 1];

for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
sumMatrix[i][j + 1] = sumMatrix[i][j] + matrix[i][j];
}
}
}

public int sumRegion(int row1, int col1, int row2, int col2) {

if (sumMatrix == null || row1 < 0 || row2 < 0 || col1 < 0
|| col2 < 0 || row1 >= sumMatrix.length - 1
|| row2 >= sumMatrix.length - 1
|| col1 >= sumMatrix[0].length - 1
|| col2 >= sumMatrix[0].length - 1 || row1 > row2
|| col1 > col2) {
return Integer.MIN_VALUE;
}

long rt = 0;

for (int i = row1; i <= row2; i++) {
rt += sumMatrix[i][col2 + 1] - sumMatrix[i][col1];
}

return (int) rt;
}

class NumMatrix {
private int[][] sums;

public NumMatrix(int[][] matrix) {
int m = matrix.length;
if (m > 0) {
int n = matrix[0].length;
sums = new int[m + 1][n + 1];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
sums[i + 1][j + 1] = sums[i][j + 1] + sums[i + 1][j] - sums[i][j] + matrix[i][j];
}
}
}
}

public int sumRegion(int row1, int col1, int row2, int col2) {
return sums[row2 + 1][col2 + 1] - sums[row2 + 1][col1] - sums[row1][col2 + 1] + sums[row1][col1];
}
}


// Your NumMatrix object will be instantiated and called as such:
// NumMatrix numMatrix = new NumMatrix(matrix);
// numMatrix.sumRegion(0, 1, 2, 3);
// numMatrix.sumRegion(1, 2, 3, 4);
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
19 changes: 19 additions & 0 deletions solution/0300-0399/0304.Range Sum Query 2D - Immutable/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class NumMatrix:

def __init__(self, matrix: List[List[int]]):
m = len(matrix)
if m > 0:
n = len(matrix[0])
self.sums = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m):
for j in range(n):
self.sums[i + 1][j + 1] = self.sums[i][j + 1] + \
self.sums[i + 1][j] - self.sums[i][j] + matrix[i][j]

def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
return self.sums[row2 + 1][col2 + 1] - self.sums[row2 + 1][col1] - self.sums[row1][col2 + 1] + self.sums[row1][col1]


# Your NumMatrix object will be instantiated and called as such:
# obj = NumMatrix(matrix)
# param_1 = obj.sumRegion(row1,col1,row2,col2)

0 comments on commit ed54ca7

Please sign in to comment.