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 ...
随机推荐
- python openpyxl模块实现excel的读取,新表创建及原数据表追加新数据
当实际工作需要把excel表的数据读取出来,或者把一些统计数据写入excel表中时,一个设计丰富,文档便于寻找的模块就会显得特别的有吸引力,本文对openpyxl模块的一些常见用法做一些记录,方便工作 ...
- WUSTOJ 1335: Similar Word(Java)
题目链接:1335: Similar Word Description It was a crummy day for Lur. He failed to pass to the CET-6 (Col ...
- Linux查询命令帮助信息(知道)
方法一 command --help 方法二 man command 操作涉及到的按键: 空格键:显示手册的下一屏 Enter键:一次滚动手册的一行 b:回滚一屏 f:前滚一屏 q:退出 结果基本上全 ...
- 音量调节条-封装通用的ProgressBar组件
import React, { Component } from 'react' import PropTypes from 'prop-types' import assign from 'obje ...
- Luogu5401 CTS2019珍珠(生成函数+容斥原理+NTT)
显然相当于求有不超过n-2m种颜色出现奇数次的方案数.由于相当于是对各种颜色选定出现次数后有序排列,可以考虑EGF. 容易构造出EGF(ex-e-x)/2=Σx2k+1/(2k+1)!,即表示该颜色只 ...
- dB分贝计算
1. 定义 dB-表征相对值的大小的单位,即两个电.声功率之比或者电流.电压.音量之比,是一种测相对大小的单位. 1.1 电.声功率之比——10lg(x/y) x.y分别表示两个欲比较的功率值. 例如 ...
- ASP.NET Core中防跨站点请求伪造
CSRF(Cross-site request forgery)利用了web中用户身份验证的一个漏洞:简单的身份验证只能保证请求发自某个用户的浏览器,却不能保证请求本身是用户自愿发出的. 例子 在某个 ...
- kvm第四章-- 虚拟化网络管理
- 【转载】 Sqlserver使用Left函数从最左边开始截取固定长度字符串
在Sqlserver数据库的字符串操作中,截取字符串是最常见的操作,sql server提供了3个常用截取字符串方法,LEFT().RIGHT().SUBSTRING(),如果从第一个字符即最左边位置 ...
- 正则表达式字符&使用
正则详细解说:https://juejin.im/post/5965943ff265da6c30653879 一.正则表达式中的字符含意 \ 做为转义,即通常在"\"后面的字符不按 ...