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 4/3 页面基础模板 与 兼容旧版本浏览器
    Asp.net core 项目实战 新闻网站+后台 源码、设计原理 、视频教程
    C# 数据类型转换 显式转型、隐式转型、强制转型
    C# 多维数组 交错数组的区别,即 [ , ] 与 [ ][ ]的区别
    C#/Entity Frame Core 使用Linq 进行分页 .Skip() .Take() 的使用方法
    利用 Xunsearch 搭建搜索引擎、内容搜索实战
    Delphi
    Python
    Python
    Python
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5787375.html
Copyright © 2011-2022 走看看