zoukankan      html  css  js  c++  java
  • LeetCode-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

    Analysis:2D BIT Practice.

    NOTE: CAREFULL about the gap between BIT's index and normal matrix index. We need be consistent when define functions.

    Solution:

     1 public class NumMatrix {
     2     int[][] sums;
     3     int[][] nums;
     4     int rows, cols;
     5 
     6     public NumMatrix(int[][] matrix) {
     7         if (matrix.length==0 || matrix[0].length==0) return;
     8 
     9         rows = matrix.length;
    10         cols = matrix[0].length;
    11         sums = new int[rows+1][cols+1];
    12         nums = new int[rows][cols];
    13         for (int i=0;i<rows;i++)
    14             for (int j=0;j<cols;j++){
    15                 update(i,j,matrix[i][j]);
    16             }
    17     }
    18 
    19     public void update(int row, int col, int val) {
    20         if (rows==0 || cols==0) return;
    21         
    22         int delta = val - nums[row][col];
    23         nums[row][col] = val;
    24         for (int x=row+1;x<=rows;x = x + (x & -x))
    25             for (int y=col+1;y<=cols;y = y + (y & -y)){
    26                 sums[x][y] += delta;
    27             }
    28     }
    29 
    30     public int sumRegion(int row1, int col1, int row2, int col2) {
    31         if (rows==0 || cols == 0) return 0;
    32         
    33         return sum(row2,col2) - sum(row2,col1-1) - sum(row1-1,col2) + sum(row1-1,col1-1);
    34     }
    35 
    36     // @row and @col still stand for the index in @nums not @sums, so we always perform index transformation in loops.
    37     public int sum(int row, int col){
    38         int sum = 0;
    39         for (int x = row+1; x>0; x -= (x & -x))
    40             for (int y = col+1; y>0; y -= (y & -y)){
    41                 sum += sums[x][y];
    42             }
    43         return sum;
    44     }
    45 }
    46 
    47 
    48 // Your NumMatrix object will be instantiated and called as such:
    49 // NumMatrix numMatrix = new NumMatrix(matrix);
    50 // numMatrix.sumRegion(0, 1, 2, 3);
    51 // numMatrix.update(1, 1, 10);
    52 // numMatrix.sumRegion(1, 2, 3, 4);
  • 相关阅读:
    使用bootstrap建立响应式网页——通栏轮播图(carousel)
    java的HashMap与ConcurrentHashMap
    JVM性能调优
    Spring注解@Component、@Repository、@Service、@Controller区别
    Java过滤器与SpringMVC拦截器之间的关系与区别
    Java线程(二):线程同步synchronized和volatile
    Java线程(一):线程安全与不安全
    MySQL 加锁处理分析
    Spring 事务机制详解
    使用jsonp跨域调用百度js实现搜索框智能提示,并实现鼠标和键盘对弹出框里候选词的操作【附源码和在线测试地址】
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5787375.html
Copyright © 2011-2022 走看看