[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 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
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12
题目
给定元素不变的矩阵,求各种子矩阵和。
思路
Given matrix = [
[3, 0, 1, 4, 2],
[5, 6, 3, 2, 1],
[1, , 0, 1, 5],
[4, 1, 0, 1, 7],
[1, 0, 3, , 5]
] sumRegion(2, 1, 4, 3) -> 8
(2,1) 为黄色range左上角的坐标, 所在坐标对应的点为2
(4,3) 为黄色range右下角的坐标, 所在坐标对应的点为0
黄色range中 2 + 0 + 0 + 1 + 0 + 1 + 0 + 3 + 0 = 8 比如, input matrix为
2 0 -3 4
6 3 2 -1
5 4 7 3
2 -6 8 1
多加一行一列方便写code,变成dp matrix为
0 0 0 0 0
2 0 -3 4
6 3 2 -1
5 4 7 3
2 -6 8 1
开始fill dp matrix
dp[i][j]表示sum of rectangle from (0,0) to matrix (i-1, j-1)
0 0 0 0 0
2 2 -1 3 //-> first row: easy to fill(累加)
0 0 0 0 0
2 -1 3 0 15
// -> first col: easy to fill(累加)
0 0 0 0 0
0 2 2 -1 3
0 8 X -> dp[i][j] = dp[i-1][j] // 正上方 2
0 13 + dp[i][j-1] // 正左方 8
0 15 + matrix [i-1][j-1] // input matrix 该位置值
- dp[i-1][j-1] // 左上角 2 ,重复加了两次需要减去一次
代码
class NumMatrix {
private int[][] dp; /* 1.build and fill dp matrix in O(m*n) time */
public NumMatrix(int[][] matrix) {
int row = 0;
int col = 0;
if (matrix.length != 0) {
row = matrix.length;
col = matrix[0].length;
}
dp = new int[row + 1][col + 1];
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + matrix[i - 1][j - 1] - dp[i - 1][j - 1];
}
} } /*2. query in O(1) time */
public int sumRegion(int row1, int col1, int row2, int col2) {
/* coz dp matrix has size 1 greater one more than original matrix*/
row1++;
col1++;
row2++;
col2++;
return dp[row2][col2] - dp[row1 - 1][col2] - dp[row2][col1 - 1] + dp[row1 - 1][col1 - 1];
}
}
代码
class NumMatrix {
private int[][] dp; /* 1.build and fill dp matrix in O(m*n) time */
public NumMatrix(int[][] matrix) {
int row = 0;
int col = 0;
if (matrix.length != 0) {
row = matrix.length;
col = matrix[0].length;
}
dp = new int[row + 1][col + 1];
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + matrix[i - 1][j - 1] - dp[i - 1][j - 1];
}
} } /*2. query in O(1) time */
public int sumRegion(int row1, int col1, int row2, int col2) {
/* coz dp matrix has size 1 greater one more than original matrix*/
row1++;
col1++;
row2++;
col2++;
return dp[row2][col2] - dp[row1 - 1][col2] - dp[row2][col1 - 1] + dp[row1 - 1][col1 - 1];
}
}
[leetcode]304. Range Sum Query 2D - Immutable二维区间求和 - 不变的更多相关文章
- [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 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变(C++/Java)
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- 304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...
- [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 ...
- 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 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】304. Range Sum Query 2D - Immutable
Range Sum Query 2D - Immutable Given a 2D matrix matrix, find the sum of the elements inside the rec ...
- [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】304. Range Sum Query 2D - Immutable 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...
随机推荐
- cordova- cordova-plugin-splashscreen启动页面和图标的设置
https://www.cnblogs.com/a418120186/p/5856371.html
- VsCode 使用习惯设置(备份)
{ "window.menuBarVisibility": "toggle", "workbench.statusBar.visible": ...
- 【转】解决Eclipse中SVN版本信息不显示的问题
eclipse 中使用 svn 插件,原本正常,未作任何更改,最近几天突然eclipse 中查看文件时,文件后面的 版本号 . 文件的状态图标 等等都不见了.以为有插件冲突,卸载了好多其他的相关的插件 ...
- Android app 性能优化的思考--性能卡顿不好的原因在哪?
说到 Android 系统手机,大部分人的印象是用了一段时间就变得有点卡顿,有些程序在运行期间莫名其妙的出现崩溃,打开系统文件夹一看,发现多了很多文件,然后用手机管家 APP 不断地进行清理优化 ,才 ...
- CHAR 和VARCHAR的区别
CHAR(10)是不可变长度为10的字符串,占的存储空间始终为10个字符的长度,而VARCHAR(10)是可变长度的字符串,故而可以节省空间.例如:储存"aaaaabbbbb",则 ...
- 30.深入理解abstract class和interface
- unity3d assetbundle打包策略
由于assetbundle打包存在依赖的问题,所有资源要进行合理的分包 零.代码 代码都放在本地,包括NGUI等插件的代码.shader代码(内置的shader无需打包,而自定义的shader还是需要 ...
- hdu1799-循环多少次?-(组合恒等式)
循环多少次? Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- kibana test
https://www.cnblogs.com/yiwangzhibujian/p/7137546.html curl -XPUT http://localhost:9200/shakespeare ...
- yum except KeyboardInterrupt, e: 错误
在上一篇升级python的时候的,使用yum时,出现以下错误 [root@localhost bin]# yum File "/usr/bin/yum", line 30 ...