LeetCode 308. Range Sum Query 2D - Mutable
原题链接在这里: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);
*/
LeetCode 308. Range Sum Query 2D - Mutable的更多相关文章
- 308. Range Sum Query 2D - Mutable
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- LeetCode Range Sum Query 2D - Mutable
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
- [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 ...
- [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 ...
随机推荐
- [转帖]TPC-C解析系列01_TPC-C benchmark测试介绍
TPC-C解析系列01_TPC-C benchmark测试介绍 http://www.itpub.net/2019/10/08/3334/ 学习一下. 自从蚂蚁金服自研数据库OceanBase获得TP ...
- 【算法】js实现最短时间走完不同速度的路程
题目: 现在有一条公路,起点是0公里,终点是100公里.这条公路被划分为N段,每一段有不同的限速.现在他们从A公里处开始,到B公里处结束.请帮他们计算在不超过限速的情况下,最少需要多少时间完成这段路程 ...
- Spring Boot开启Druid数据库监控功能
Druid是一个关系型数据库连接池,它是阿里巴巴的一个开源项目.Druid支持所有JDBC兼容的数据库,包括Oracle.MySQL.Derby.PostgreSQL.SQL Server.H2等.D ...
- java面向函数编程简单应用
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.functio ...
- C++基础(静态数据成员和静态成员函数)
[简介] 1.静态数据成员在类中声明,在源文件中定义并初始化: 2.静态成员函数没有this指针,只能访问静态数据成员: 3.调用静态成员函数:(1)对象.(2)直接调用: 4.静态成员函数的地址可用 ...
- Github相关问题集锦
问题 对于我们国内用户,有时候浏览器在进入github网站时,会出现无法加载或加载很慢的问题,针对这一问题,很是头疼,为解决这一问题,在网上搜索到的解决方法如下: 解决方案 修改hosts文件,在ho ...
- SQL Server元数据查询
原文:SQL Server元数据查询 1.查询触发器的信息 --查询触发器的信息 select name, --触发器名称 (select name from sys.objects where ob ...
- 记https在Android浏览器无法访问
问题描述 M站静态资源单独配置的https域名,在Android原生浏览器里面打开之后提示证书不安全,在chrome.UC之类的浏览器之下,静态资源都能够正常访问 问题原因 CA证书链不完整 http ...
- Hystrix 熔断器
Hystrix 是Netflix开源的一个延迟和容错库,用于隔离访问远程服务,防止出现级联失败 一.Hystrix 的定义 二.Hystrix 的原理 在分布式式系统中应用熔断器后,服务调用方可以自己 ...
- 时间都去哪儿了?开源一个统计iPhone上App运行时间和打开次数的小工具【iOS8已失效】
如今,大家每天都有大量时间花在手机上,但是,大家有没有想过自己的时间都花在哪些App上了呢?相信很多人都有这样的需求,不过iOS系统本身并不能显示每个App的运行时间和次数,因此,本人写了这样一个小工 ...