Skip to content

Commit 907c80e

Browse files
committed
Solution for: Num matrix
1 parent 0826a22 commit 907c80e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/leetcode/NumMatrix.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package leetcode;
2+
3+
public class NumMatrix {
4+
private int[][] matrix;
5+
public NumMatrix(int[][] matrix) {
6+
this.matrix = matrix;
7+
}
8+
9+
public int sumRegion(int row1, int col1, int row2, int col2) {
10+
int sum=0;
11+
for(int i= row1; i<=row2;i++){
12+
for(int j= col1; j<=col2;j++){
13+
sum = sum+ matrix[i][j];
14+
}
15+
}
16+
return sum;
17+
}
18+
19+
public static void main(String[] args) {
20+
int[][] matrix = new int[][]{{3, 0, 1, 4, 2},{5, 6, 3, 2, 1},
21+
{1, 2, 0, 1, 5},{4, 1, 0, 1, 7},{1, 0, 3, 0, 5} };
22+
NumMatrix test = new NumMatrix(matrix);
23+
System.out.println(test.sumRegion(2, 1, 4, 3));
24+
System.out.println(test.sumRegion(1, 1, 2, 2));
25+
System.out.println(test.sumRegion(1, 2, 2, 4));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)