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 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.
分析:
给定一个二维矩阵,求其子矩阵内所有元素的和。
如果每次调用sumRegion,遍历范围内所有元素的和,这种办法可行但是并不可取。我们先来看下面这个图。

上图是求解一个矩形面积的图画 展示,可以清楚的理解各个矩形块之间的关系,我们把矩形内的元素和比作矩形的面积。定义dp为二维数组,dp[i][j]是以(i, j)元素为右下角,可以求得一个子矩阵内所有元素的和。那么我们把dp[i][j]的值想象成上图中矩形的面积,大矩形的面积=去除右边一列的面积+去除下边一行的面积-重复的地方(左上角的面积)+当前小矩形块的面积。
此时当前的小矩形块就是所给定的矩阵(i, j)位置的元素。
去除右边一列的面积也就是以(i, j-1)元素为右下角的矩阵的元素和,实际上就是dp[i][j-1]。
去除下边一行的面积就是以(i-1, j)元素为右下角的矩阵的元素和,实际上就是dp[i-1][j]。
左上角的面积则是以(i-1, j-1)元素为右下角的矩阵的元素和,实际上就是dp[i-1][j-1]。
借此我们可以在O(mn)的时间内求的dp矩阵,接下来要求任意给定子矩阵的所有元素和,看下图。

同样还是将矩阵内的元素和比作矩形的面积,此时右下角为我们所要求的矩形面积=大矩形面积-去除右边区域的面积+去除下边区域的面积+重复的地方(左上角的面积)。
此时我们就可以利用上边所求的dp数组来快速求的结果。由于给了范围,也就是x1,y1,x2,y2,通过上面的图我们不难看出各个面积区域之间的关系。
实际上最后的结果也就是右下角的矩形面积。那么
大矩形面积也就是以x2, y2为右下角的矩阵的元素和,实际上就是dp[x2][y2]。
去除右边区域的面积就是以x2, y1-1为右下角的矩阵的元素和,实际上就是dp[x2][y1-1]。
去除下边区域的面积就是以x1-1, y2为右下角的矩阵的元素和,实际上就是dp[x1-1][y2]。
左上角的面积就是以x1-1, y1-1为右下角的矩阵的元素和,实际上就是dp[x1-1][y1-1]。
最后的结果就是dp[x2][y2] - dp[x2][y1-1] - dp[x1-1][y2] + dp[x1-1][y1-1]。
小技巧就是开辟的dp数组长宽可以比原数组多1,这样就不用了处理边界条件了。
程序:
C++
class NumMatrix {
public:
NumMatrix(vector<vector<int>>& matrix) {
if(matrix.empty())
return;
int m = matrix.size();
int n = matrix[].size();
dp = vector<vector<int>>(m+, vector<int>(n+, ));
for(int i = ; i <= m; ++i){
for(int j = ; j <= n; ++j){
dp[i][j] = dp[i-][j] + dp[i][j-] - dp[i-][j-] + matrix[i-][j-];
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
return dp[row2+][col2+] - dp[row2+][col1] - dp[row1][col2+] + dp[row1][col1];
}
private:
vector<vector<int>> dp;
};
Java
class NumMatrix {
public NumMatrix(int[][] matrix) {
if(matrix.length == 0 || matrix == null)
return;
int m = matrix.length;
int n = matrix[0].length;
dp = new int[m+1][n+1];
for(int i = 1; i <= m; ++i){
for(int j = 1; j <= n; ++j){
dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] + matrix[i-1][j-1];
}
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
return dp[row2+1][col2+1] - dp[row2+1][col1] - dp[row1][col2+1] + dp[row1][col1];
}
private int[][] dp;
}
LeetCode 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变(C++/Java)的更多相关文章
- [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 二维区域和检索 - 不可变
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (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] 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(递推)
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 ...
- Java实现 LeetCode 304 二维区域和检索 - 矩阵不可变
304. 二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). Range Sum Qu ...
- Leetcode 304.二维区域和检索-矩阵不可变
二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, c ...
随机推荐
- $vjudge-dp$专题题解
因为感觉题解写不了多少,,,就懒得一道道题目慢慢写了,汇总了算了$QAQ$ 昂然后因为我估计以后还会有些什么$dp$专题啊$balabala$的,,,然后谢总肯定又会建一堆小组啥的,,,所以还是放个链 ...
- 「Luogu P3866」[TJOI2009]战争游戏 解题报告
题面 好难表述啊~ 在n*m的矩阵上,有一些大兵(为0),一些空地(一个正整数),障碍物(-1),现在摧毁一些空地,使所有大兵不能走出矩阵去(代价为表示空地的整数),求最小代价 思路: 网络流最小割 ...
- 1034 有理数四则运算 (20 分)C语言
题目描述 本题要求编写程序,计算2个有理数的和.差.积.商. 输入描述: 输入在一行中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整 ...
- Linux 查看实时网卡流量的方法 sar nload iftop
1.sar 计量脚本 sar(System Activity Reporter 系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读 ...
- C#录制视频
这是一个使用C#语言制作的录制框架,支持录制桌面,多屏,声音,摄像头,某个应用程序的界面 1.安装 使用此框架需要安装扩展包Kogel.Record,可以Nuget上搜索 或者使用Nuget命令 In ...
- 前端页面表格排序 jQuery Table 基础
通常来说, 排序的方式有两种, 一种是我们在查询的时候就排好序,然后将数据渲染到前台页面上, 但是这样做有个弊端,就是在争对做好了缓存处理的系统, 在查询相同数据的时候进行排序,可能不能成功, 因为进 ...
- Vue.js 入门 --- vue.js 安装
本博文转载 https://blog.csdn.net/m0_37479246/article/details/78836686 Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据 ...
- 【转】ArcGIS Server 10.1 动态图层—添加栅格
本文将介绍如何通过arcgisserver10.1动态图层添加栅格影像.与添加矢量数据不同的是,天际栅格用到了RasterDataSource接口,如下所示 <esri:DynamicLayer ...
- Centos7下创建和管理用户
centos服务管理主要命令是systemctl,centos7的服务不再放在/etc/init.d/下;而放在/usr/lib/systemd/system下,centos7系统中systemctl ...
- 用markdown写博客,看这一篇就够了
0. 前言 记得上次用markdown写博客,尽管我有markdown使用经验,但第一篇markdown博客还是不得已的"回滚"了. 传送门:记录一下第一次用markdown写博客 ...