-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update leetcode solutions: No.0304. Range Sum Query 2D - Immutable
- Loading branch information
Showing
4 changed files
with
131 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 24 additions & 44 deletions
68
solution/0300-0399/0304.Range Sum Query 2D - Immutable/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
solution/0300-0399/0304.Range Sum Query 2D - Immutable/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |