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

    原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/

    题目:

    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

    题解:

    用到了二维binary index tree. 

    Time Complexity: builder O(mnlogmn). update O(logmn). sumRange O(logmn). m = matrix.length. n = matrix[0].length.

    Space : O(mn).

    AC Java:

     1 public class NumMatrix {
     2     int [][] bit;
     3     int [][] matrix;
     4     
     5     public NumMatrix(int[][] matrix) {
     6         if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
     7             return;
     8         }
     9         int m = matrix.length;
    10         int n = matrix[0].length;
    11         this.bit = new int[m+1][n+1];
    12         this.matrix = new int[m][n];
    13         for(int i = 0; i<m; i++){
    14             for(int j = 0; j<n; j++){
    15                 update(i, j, matrix[i][j]);
    16             }
    17         }
    18     }
    19     
    20     public void update(int row, int col, int val) {
    21         int diff = val - this.matrix[row][col];
    22         this.matrix[row][col] = val;
    23         for(int i = row+1; i<bit.length; i+=(i&-i)){
    24             for(int j = col+1; j<bit[0].length; j+=(j&-j)){
    25                 this.bit[i][j] += diff;
    26             }
    27         }
    28     }
    29     
    30     public int sumRegion(int row1, int col1, int row2, int col2) {
    31         return getSum(row2+1, col2+1) - getSum(row1, col2+1) - getSum(row2+1, col1) + getSum(row1, col1);
    32     }
    33     
    34     private 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 += this.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  */

    类似Range Sum Query - Mutable

  • 相关阅读:
    汇编实验四
    汇编实验三
    C语言常用标准库函数
    实验一
    NGUI的缓动
    unity之Hashtable ArrayList List
    有关Unity 的一些 笔记之
    有关Unity 的一些 笔记之场景
    有关Unity 的一些 笔记之prefab
    有关Unity 的一些 笔记之消息事件
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/5246473.html
Copyright © 2011-2022 走看看