LeetCode 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 Range Sum Query 2D - Mutable的更多相关文章
- [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] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- 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 308. Range Sum Query 2D - Mutable
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
- 308. Range Sum Query 2D - Mutable
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- [LeetCode] Range Sum Query 2D - Immutable
Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fac ...
- [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 ...
随机推荐
- BZOJ4657 : tower
显然只有横向和纵向的两个炮塔才有可能冲突. 考虑最小割,将每个炮塔所有能攻击到的位置建点,相邻之间连无穷的边,表示前缀和关系,即选了一个点,就必须要选所有比它近的点. 属于横向炮塔的点向$S$连边,容 ...
- [Leetcode] Word BreakII
Question: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence w ...
- cocos2dx中设置横竖版
IOS目录中RootViewController.mm//显示竖屏- (BOOL) shouldAutorotate { return NO;} //显示横屏- (BOOL) shouldAutoro ...
- AngularJS 乱记
1. 前端简单逻辑 <title data-ng-bind="{true:' ('+notice_count+') '}[notice_count > 0]+{true:glob ...
- nodejs获取当前url和url参数值
//需要使用的模块 http url 当前url http://localhost:8888/select?aa=001&bb=002 var http = require('http ...
- 浅析-博客Ping服务
简介:PING服务是博客站点向博客目标网站.搜索引擎等发出的博客内容更新通知服务,然后博客目标网站.搜索引擎就会及时的索引.收录以及传播您的博客内容. PING原理 PING 服务是博客站点向博客目标 ...
- iOS性能优化:Instruments使用实战
iOS性能优化:Instruments使用实战 最近采用Instruments 来分析整个应用程序的性能.发现很多有意思的点,以及性能优化和一些分析性能消耗的技巧,小结如下. Instrument ...
- c++用双向链表实现模板栈
可直接编译运行,其中方法status为形象的显示出栈的结构: // visual stack , need define "cout<<" #include < ...
- 学习Jquery
早就听说了Jquery的大名,一直没有细心的学习一下,通过阅读收集的一些资料,感觉Jquery真的很强大.决定开始自己的学习Jquery之旅.在这里不是为大家讲解Jquery(深知水平有限),只是将自 ...
- bootstrap input框清空
<!DOCTYPE HTML> <html> <head> <link href="http://netdna.bootstrapcdn.com/t ...