原题链接在这里: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).


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:

 public class NumMatrix {
int [][] bit;
int [][] matrix; public NumMatrix(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return;
}
int m = matrix.length;
int n = matrix[0].length;
this.bit = new int[m+1][n+1];
this.matrix = new int[m][n];
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
update(i, j, matrix[i][j]);
}
}
} public void update(int row, int col, int val) {
int diff = val - this.matrix[row][col];
this.matrix[row][col] = val;
for(int i = row+1; i<bit.length; i+=(i&-i)){
for(int j = col+1; j<bit[0].length; j+=(j&-j)){
this.bit[i][j] += diff;
}
}
} public int sumRegion(int row1, int col1, int row2, int col2) {
return getSum(row2+1, col2+1) - getSum(row1, col2+1) - getSum(row2+1, col1) + getSum(row1, col1);
} private int getSum(int row, int col){
int sum = 0;
for(int i = row; i>0; i-=(i&-i)){
for(int j = col; j>0; j-=(j&-j)){
sum += this.bit[i][j];
}
}
return sum;
}
} /**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* obj.update(row,col,val);
* int param_2 = obj.sumRegion(row1,col1,row2,col2);
*/

类似Range Sum Query - Mutable.

LeetCode 308. Range Sum Query 2D - Mutable的更多相关文章

  1. 308. Range Sum Query 2D - Mutable

    题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  2. Range Sum Query 2D - Mutable & Immutable

    Range Sum Query 2D - Mutable Given a 2D matrix matrix, find the sum of the elements inside the recta ...

  3. [Locked] Range Sum Query 2D - Mutable

    Range Sum Query 2D - Mutable Given a 2D matrix matrix, find the sum of the elements inside the recta ...

  4. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  5. Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  6. [LeetCode] 304. Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  7. LeetCode Range Sum Query 2D - Mutable

    原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...

  8. [leetcode]304. Range Sum Query 2D - Immutable二维区间求和 - 不变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  9. [Swift]LeetCode308. 二维区域和检索 - 可变 $ Range Sum Query 2D - Mutable

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

随机推荐

  1. Quartz.Net—IJob特性

    IJob默认情况下是无状态的,和其他系统没有关系  特别是job里面的jobdata每次都是新的.可以无限扩展. PersistJobDataAfterExecution JobData持久化 Job ...

  2. 长乐培训Day6

    T1 数列 题目 [题目描述] [输入格式] [输出格式] [输入样例] [输出样例] [数据规模] 如上所述. 解析 身为T1,居然比T4还难......让我怎么办......以下为巨佬题解: 我猜 ...

  3. eclise -The method onClick(View) of type new View.OnClickListener(){} must override a superclass method 在做arcgis android开发的时候,突然遇到这种错误,The method onClick(View) of type new View.OnClickListener(){} mus

    eclise -The method onClick(View) of type new View.OnClickListener(){} must override a superclass met ...

  4. golang开发:环境篇(五)实时加载工具gin的使用

    gin 工具是golang开发中非常有用且有效的工具,有效的提高了开发调试go程序的效率. 为什么要使用gin 我们知道golang是编译型语言,这就表示go程序的每次改动,如果需要查看改动结果都必须 ...

  5. oracle-3-Linux-11g安装-静默安装

    oracle下载地址:https://www.oracle.com/database/technologies/112010-linx8664soft.html 系统是最小化安装的Centos7.2 ...

  6. NetLink通信原理研究、Netlink底层源码分析、以及基于Netlink_Connector套接字监控系统进程行为技术研究

    1. Netlink简介 0x1:基本概念 Netlink是一个灵活,高效的”内核-用户态“.”内核-内核“.”用户态-用户态“通信机制.通过将复杂的消息拷贝和消息通知机制封装在统一的socket a ...

  7. javascript 之 扩展对象

    注意点:在js中常见的几种方进行扩展 第一种:ES6提供的 Object.assign(); 第二种:ES5提供的 extend()方法 第三种:Object对象提供的 defineProperty( ...

  8. Asp.Net 加载不同项目程序集

    我们做项目时有时候不想添加别的项目的引用,但是那个项目又必须在 Global 中进行注册 最常见的就是插件机制,参考: https://shazwazza.com/post/Developing-a- ...

  9. Springboot笔记01——Springboot简介

    一.什么是微服务 在了解Springboot之前,首先我们需要了解一下什么是微服务. 微服务是一种架构风格(服务微化),是martin fowler在2014年提出来的.微服务简单地说就是:一个应用应 ...

  10. 图片上传怎么用File接受文件

    xl_echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.——这才是真正的堪称强大!! - ...