-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 33
30 lines (26 loc) · 842 Bytes
/
Day 33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class NumMatrix {
final int[][] prefix;
public NumMatrix(int[][] matrix) {
this.prefix = new int[matrix.length +1][matrix[0].length +1];
for(int i =0; i < matrix.length; i++) {
int rowSum =0;
for(int j =0; j < matrix[0].length; j++) {
rowSum += matrix[i][j];
prefix[i+1][j+1] = rowSum;
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
int sum = 0;
for(int i = row1+1; i <= row2+1; i++) {
int tempsum = prefix[i][col2+1] - prefix[i][col1];
sum += tempsum;
}
return sum;
}
}
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/