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
Note:
- You may assume that the matrix does not change.
- There are many calls to sumRegion function.
- You may assume that row1 ≤ row2 and col1 ≤ col2.
链接: http://leetcode.com/problems/range-sum-query-2d-immutable/
题解:
二维矩阵求Range Sum。这题我们也是用DP,不过dp的方法是: dp[i][j]等于从坐标matrix[0][0]到matrix[i - 1][j - 1]中所有元素的和。 这样我们就可以用中小学时计算矩形重叠面积的方法来计算出我们想要的结果。
Time Complexity - O(n2), Space Complexity - O(n2)。
public class NumMatrix {
private int[][] sum;
public NumMatrix(int[][] matrix) {
if(matrix == null || matrix.length == 0) {
return;
}
int rowNum = matrix.length, colNum = matrix[0].length;
sum = new int[rowNum + 1][colNum + 1];
for(int i = 1; i < sum.length; i++) {
for(int j = 1; j < sum[0].length; j++) {
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + matrix[i - 1][j - 1];
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
return sum[row2 + 1][col2 + 1] - sum[row1][col2 + 1] - sum[row2 + 1][col1] + sum[row1][col1];
}
}
// Your NumMatrix object will be instantiated and called as such:
// NumMatrix numMatrix = new NumMatrix(matrix);
// numMatrix.sumRegion(0, 1, 2, 3);
// numMatrix.sumRegion(1, 2, 3, 4);
Reference:
https://leetcode.com/discuss/69047/clean-and-easy-to-understand-java-solution
https://leetcode.com/discuss/69424/clean-c-solution-and-explaination-o-mn-space-with-o-1-time
https://leetcode.com/discuss/69144/c-with-helper
https://leetcode.com/discuss/69054/dp-java-solution
https://leetcode.com/discuss/69045/sharing-my-python-solution
https://leetcode.com/discuss/71297/my-java-solution-only-used-6-ms
https://leetcode.com/discuss/69611/share-my-short-java-solution
https://leetcode.com/discuss/69435/my-c-solution-o-n-2-setup-o-1-sumregion
https://leetcode.com/discuss/69141/range-sum-query-2d-mutable-c-tree-array
https://leetcode.com/discuss/69137/short-python-solution-exactly-same-that-solves-range-query
https://leetcode.com/discuss/69117/c-solution-o-1-for-sumregion-function
304. Range Sum Query 2D - Immutable的更多相关文章
- 【刷题-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] 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...
- [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 ...
- 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
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) ,右 ...
随机推荐
- C中的一些函数
简述:printf.sprintf函数 转载自http://www.cnblogs.com/adslg/archive/2008/08/22/1274164.html 部分进行了修改,参考http:/ ...
- 嵌套回调异步与$.Deferred异步
HTML: <input type="button" id="btn1" value="嵌套回调异步"> <input t ...
- MVC4 网站发布(整理 + 部分转载 + 部分问题收集和解决方案)
网站发布步骤: 这部分是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接 ...
- 图片上传前的预览(PHP)
1.先创建一个file表单域,我们需要用它来浏览本地文件.<form name="form1" id="form1" method="post& ...
- 了解Git
对于计算机软件初学者来说Git并没有太多了解, 以前没有接触过,但是老师说对其进行了解,也没有什么概念,只有通过上网进行了解 . 了解到的大概内容如下: ...
- JS 学习笔记--13---原型
练习中使用的是IE11,如果有错误之处,还请各位朋友多多指教.本文关于原型难以描述,故多用代码展示 原型是JS中一个很重要的概念,也是JS中一个难点,语言上难以描述,原型对象的属性和方法叫做原型属性和 ...
- 疑难杂症rendering(对角线上的线)
postprocess全屏特效 对角线有条线 明显和buffer有关因为线由小的东西组成 就像之前没清空buffer产生的马赛克 beginscene时 clearmask 设0 ---------- ...
- Curse of Dimensionality
Curse of Dimensionality Curse of Dimensionality refers to non-intuitive properties of data observed ...
- ubuntu修改ip、网关、dns等
一.使用命令设置Ubuntu IP地址 1.修改配置文件blacklist.conf禁用IPV6 sudo vi /etc/modprobe.d/blacklist.conf 表示用vi编辑器(也可以 ...
- LA 3350
The NASA Space Center, Houston, is less than 200 miles from San Antonio, Texas (the site of the ACM ...