题目:

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:

  1. You may assume that the matrix does not change.
  2. There are many calls to sumRegion function.
  3. 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)的更多相关文章

  1. [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 ...

  2. 304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. Java实现 LeetCode 304 二维区域和检索 - 矩阵不可变

    304. 二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). Range Sum Qu ...

  9. Leetcode 304.二维区域和检索-矩阵不可变

    二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, c ...

随机推荐

  1. 你确定你了解什么是linux系统?

    1.什么是linux发行版 就Linux的本质来说,它只是操作系统的核心,负责控制硬件.管理文件系统.程序进程等,并不给用户提供各种工具和应用软件.所谓工欲善其事,被必先利其器,一套在优秀的操作系统核 ...

  2. 如何对N个接口按比例压测

    随着微服务盛行,公司的服务端项目也越来越多.单一的接口性能测试并不能准确反映某个服务的总体处理能力,在服务功能划分比较清晰的架构下,对于某一服务的总体性能测试也相对变得简单.下面分享一个对于某个模块对 ...

  3. TensorFlow——tf.contrib.layers库中的相关API

    在TensorFlow中封装好了一个高级库,tf.contrib.layers库封装了很多的函数,使用这个高级库来开发将会提高效率,卷积函数使用tf.contrib.layers.conv2d,池化函 ...

  4. MongoDB DBA 实践8-----Linux系统Mongodb分片集群部署

    在Linux系统中,主要是使用命令行进行mongodb的分片集群部署 一.先决条件 mongodb安装成功,明确路径, MongoDB的几个路径: /var/lib/mongodb /var/log/ ...

  5. 搭建自己的Online Judge

    前言 很多人对于做题有点厌烦,但是,如果让你出题给别人做那么可能会很有意思.可是,出题只能出在一些别人的OJ上,甚至只能在自己的Word文档里出.今天我教大家一个厉害点的,叫做搭建自己的Online ...

  6. 暑假提高组集训Day1 T2

    那么这一道题我在考试的时候写挂了(0分 呜呜~) 我原来的思路是广搜来骗取部分分(哈哈~) 但是我忘记了一个非常重要的问题 我广搜开的数组没有考虑负的下标 下一次考试如果再写暴力 就可以把坐标都加上一 ...

  7. 查看JVM参数

    如何查看一个正在运行中的java程序,它的某个jvm参数是否开启?具体值是多少? jps jinfo jvm的参数类型: 1.标配参数:java -version  ,java -help , jav ...

  8. 常用crud

    增:@Insert("insert into  t_user (`last_name`, `sex`) values(#{lastName}, #{sex})")   删:@Del ...

  9. 所有锁的unlock要放到try{}finally{}里,不然发生异常返回就丢了unlock了

    所有锁的unlock要放到try{}finally{}里,不然发生异常返回就丢了unlock了

  10. 《爬虫学习》(二)(urllib库使用)

    urllib库是Python中一个最基本的网络请求库.可以模拟浏览器的行为,向指定的服务器发送一个请求,并可以保存服务器返回的数据. 1.urlopen函数: 在Python3的urllib库中,所有 ...