首页 > 其他 > 详细

304. Range Sum Query 2D - Immutable

时间:2018-10-26 12:40:02      阅读:177      评论:0      收藏:0      [点我收藏+]
 1 class NumMatrix {
 2     int[][] sum;
 3     
 4     public NumMatrix(int[][] matrix) {
 5         int row = matrix.length;
 6         if(row > 0){
 7              int col = matrix[0].length;
 8              sum = new int[row+1][col+1];
 9              for(int i = 0; i < row; i++){
10                  for(int j = 0; j < col; j++){
11                      sum[i+1][j+1] = matrix[i][j] + sum[i][j+1] + sum[i+1][j] - sum[i][j]; 
12                  }
13              }
14         }
15        
16         
17     }
18     
19     public int sumRegion(int row1, int col1, int row2, int col2) {
20         if(sum.length == 0) return 0;
21         if(sum[0].length == 0) return 0;
22         return sum[row2+1][col2+1] - sum[row2+1][col1] - sum[row1][col2+1] + sum[row1][col1];
23        
24         
25     }
26 }

 

304. Range Sum Query 2D - Immutable

原文:https://www.cnblogs.com/goPanama/p/9855505.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!