zoukankan      html  css  js  c++  java
  • 308. Range Sum Query 2D

    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).

    Range Sum Query 2D
    The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.

    Example:

    Given matrix = [
      [3, 0, 1, 4, 2],
      [5, 6, 3, 2, 1],
      [1, 2, 0, 1, 5],
      [4, 1, 0, 1, 7],
      [1, 0, 3, 0, 5]
    ]
    
    sumRegion(2, 1, 4, 3) -> 8
    update(3, 2, 2)
    sumRegion(2, 1, 4, 3) -> 10
    

    Note:

    1. The matrix is only modifiable by the update function.
    2. You may assume the number of calls to update and sumRegion function is distributed evenly.
    3. You may assume that row1 ≤ row2 and col1 ≤ col2.

    本题和Range Sum Query-mutable 和Range SumQuery-2D比较类似,方法是两者的结合,二维和一维的思路是差不多的,代码如下:

     1 public class NumMatrix {
     2     int[][] BIT;
     3     int[][] nums;
     4     int m;
     5     int n;
     6     public NumMatrix(int[][] matrix) {
     7         if(matrix==null||matrix.length==0||matrix[0].length==0) return;
     8         m = matrix.length;
     9         n = matrix[0].length;
    10         BIT = new int[m+1][n+1];
    11         nums = new int[m][n];
    12         for(int i=0;i<m;i++){
    13             for(int j=0;j<n;j++){
    14                 update(i,j,matrix[i][j]);
    15             }
    16         }
    17     }
    18     
    19     public void update(int row, int col, int val) {
    20         if(m==0||n==0) return;
    21         int diff = val-nums[row][col];
    22         nums[row][col] = val;
    23         for(int i=row+1;i<=m;i+=(i&-i)){
    24             for(int j=col+1;j<=n;j+=(j&-j)){
    25                 BIT[i][j] +=diff;
    26             }
    27         }
    28     }
    29     
    30     public int sumRegion(int row1, int col1, int row2, int col2) {
    31         if(m==0||n==0) return 0;
    32         return getSum(row2+1,col2+1)-getSum(row1,col2+1)-getSum(row2+1,col1)+getSum(row1,col1);
    33     }
    34     public int getSum(int row,int col){
    35         int sum = 0;
    36         for(int i=row;i>0;i-=(i&-i)){
    37             for(int j=col;j>0;j-=(j&-j)){
    38                 sum+=BIT[i][j];
    39             }
    40         }
    41         return sum;
    42     }
    43 }
    44 
    45 /**
    46  * Your NumMatrix object will be instantiated and called as such:
    47  * NumMatrix obj = new NumMatrix(matrix);
    48  * obj.update(row,col,val);
    49  * int param_2 = obj.sumRegion(row1,col1,row2,col2);
    50  */
  • 相关阅读:
    Mac sublime text3 安装插件
    趣题记录
    Shadow DOM及自定义标签
    JavaScript 对象部署 Iterator 接口
    JavaScript实现循环链表
    使用JavaScript实现单向链表
    nodejs深入浅出读书笔记(三)
    nodejs深入浅出读书笔记(一)
    nodejs深入浅出读书笔记(二)
    为什么要了解Event loop?(二)
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6499837.html
Copyright © 2011-2022 走看看