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 ...
随机推荐
- 【转载】你未必知道的49个CSS知识点
原文链接: https://juejin.im/post/5d3eca78e51d4561cb5dde12 虽然大多数我都会,嘻嘻.不过案例太生动了,值得收藏.
- $POJ2411\ Mondriaan's\ Dream$ 状压+轮廓线$dp$
传送门 Sol 首先状压大概是很容易想到的 一般的做法大概就是枚举每种状态然后判断转移 但是这里其实可以轮廓线dp 也就是从上到下,从左到右地放方块 假设我们现在已经放到了$(i,j)$这个位置 那么 ...
- Http请求头安全策略
今天在网上浪了许久,只是为了找一个很简单的配置,却奈何怎么都找不到. 好不容易找到了,我觉得还是记录下来的好,或许省得许多人像我一样浪费时间. 1.X-Frame-Options 如果网站可以嵌入到I ...
- C#操作注册表(简单方便,兼容X32和X64)
C#操作注册表(简单方便,兼容X32和X64) 大家好,我在这里给大家介绍本人实现的操作注册表的类,简单方便,兼容32位系统和64位系统. 一般大家用C#操作注册的方法是使用命名空间Microsoft ...
- 在.NET Core中批量注入Grpc服务
GRPC 是谷歌发布的一个开源.高性能.通用RPC服务,尽管大部分 RPC 框架都使用 TCP 协议,但其实 UDP 也可以,而 gRPC 干脆就用了 HTTP2.还有就是它具有跨平台.跨语言 等特性 ...
- JVM之GC(二)
昨天总结了GC之前要做的事情,今天介绍一下主流的GC算法. 先介绍一下几个名词: Stop The World(STW):JVM进行GC的时候总不能一边清理垃圾一边制造垃圾把,那么垃圾鉴定的准确性根本 ...
- SCU 4439 Vertex Cover|最小点覆盖
传送门 Vertex Cover frog has a graph with n vertices v(1),v(2),…,v(n)v(1),v(2),…,v(n) and m edges (v(a1 ...
- eclipse中使用postgreSQL报错( Cannot load JDBC driver class )
需求: 使用Maven插件调用PostgreSQL数据库 环境: eclipse_4.5.0+JDK_1.7+Tomcat_7.0+Maven+postgresql-9.1-901.jdbc4.jar ...
- extract函数的使用
EXTRACT(field FROM source) extract函数从日期/时间数值里抽取子域,比如年.小时等. source必须是一个timestamp, time, interval类型的值表 ...
- Shell水平测试-想学习Shell的童鞋必选必看文章
[SHELL水平测试] [OVERVIEW 篇] 有很多种 shell, 你熟悉几种? 各个 shell 的 home page 在那里? 为什么说 zsh 是目前为止功能最为强大的 shell. 为 ...