-
Notifications
You must be signed in to change notification settings - Fork 0
/
304_Range Sum Query 2D-Immutable.cpp
55 lines (49 loc) · 1.38 KB
/
304_Range Sum Query 2D-Immutable.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class NumMatrix {
public:
vector<vector<int>> matrix;
NumMatrix(vector<vector<int>> mat) {
matrix=mat;
int row=matrix.size();
int column=0;
if(row>0)
column=matrix[0].size();
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
int a=0;
int b=0;
int c=0;
if(i>0){
b=matrix[i-1][j];
}
if(j>0){
a=matrix[i][j-1];
}
if(i>0 and j>0){
c=matrix[i-1][j-1];
}
matrix[i][j]=matrix[i][j]+a+b-c;
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
int ans=0;
if(row1>0 and col1>0){
ans=matrix[row2][col2]-matrix[row2][col1-1]-matrix[row1-1][col2]+matrix[row1-1][col1-1];
}
else if(row1>0){
ans=matrix[row2][col2]-matrix[row1-1][col2];
}
else if(col1>0){
ans=matrix[row2][col2]-matrix[row2][col1-1];
}
else{
ans=matrix[row2][col2];
}
return ans;
}
};
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/