Skip to content

Commit 7140bda

Browse files
refactor 304
1 parent b9f3ffa commit 7140bda

File tree

1 file changed

+2
-27
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+2
-27
lines changed

src/main/java/com/fishercoder/solutions/_304.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 304. Range Sum Query 2D - Immutable
5-
*
6-
* Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
7-
8-
Range Sum Query 2D
9-
The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.
10-
11-
Example:
12-
Given matrix = [
13-
[3, 0, 1, 4, 2],
14-
[5, 6, 3, 2, 1],
15-
[1, 2, 0, 1, 5],
16-
[4, 1, 0, 1, 7],
17-
[1, 0, 3, 0, 5]
18-
]
19-
20-
sumRegion(2, 1, 4, 3) -> 8
21-
sumRegion(1, 1, 2, 2) -> 11
22-
sumRegion(1, 2, 2, 4) -> 12
23-
Note:
24-
You may assume that the matrix does not change.
25-
There are many calls to sumRegion function.
26-
You may assume that row1 ≤ row2 and col1 ≤ col2.
27-
*/
283
public class _304 {
294

305
public static class Solution1 {
@@ -40,14 +15,14 @@ public NumMatrix(int[][] matrix) {
4015
for (int i = 0; i < matrix.length; i++) {
4116
for (int j = 0; j < matrix[0].length; j++) {
4217
tot[i + 1][j + 1] =
43-
matrix[i][j] + tot[i + 1][j] + tot[i][j + 1] - tot[i][j];
18+
matrix[i][j] + tot[i + 1][j] + tot[i][j + 1] - tot[i][j];
4419
}
4520
}
4621
}
4722

4823
public int sumRegion(int row1, int col1, int row2, int col2) {
4924
return tot[row2 + 1][col2 + 1] - tot[row2 + 1][col1] - tot[row1][col2 + 1]
50-
+ tot[row1][col1];
25+
+ tot[row1][col1];
5126
}
5227

5328
int[][] tot;

0 commit comments

Comments
 (0)