题目:

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. 【题解】CF894E Ralph and Mushrooms (缩点)

    [题解]CF894E Ralph and Mushrooms (缩点) 这是紫?给个解方程算法 考虑一条边若可以重复遍历说明一定有环,有环的话一定会把环上的蘑菇榨干,考虑一条边从全部到榨干的贡献是多少 ...

  2. Kubernetes 会不会“杀死” DevOps?

    作者丨孙健波(天元)  阿里巴巴技术专家 导读:DevOps 这个概念最早是在 2007 年提出的,那时云计算基础设施的概念也才刚刚提出没多久,而随着互联网的逐渐普及,应用软件的需求爆发式增长,软件开 ...

  3. (01)hibernate框架环境搭建及测试

    ---恢复内容开始--- 1.创建javaweb项目 2.导包 hibernate包 hibernate\lib\required\*.jar 数据库驱动包 mysql-connector-java- ...

  4. 原生javascript实现仿QQ延时菜单

    一.实现原理 定时器和排他思想 二.代码 <!DOCTYPE html> <html> <head> <title></title> < ...

  5. Spring Cloud(一):服务注册中心Eureka

    Spring Cloud 基于 Netflix 的几个开源项目进行了封装,提供包括服务注册与发现(Eureka),智能路由(Zuul),熔断器(Hystrix),客户端负载均衡(Ribbon)等在内的 ...

  6. 【GeneXus】开发移动APP时,如何使用Canvas进行布局?

    当我们开发移动端APP的时候,经常遇到一种布局方式,那就是层级的布局,比如:一张照片我想在照片的上面显示它的名称,但不影响我照片展示的布局大小,也就是这个名称是浮在照片上的,如图: 如果要实现这样的布 ...

  7. 2019版Idea如何激活

    1.下载jar包 链接: https://pan.baidu.com/s/1w4B4_hmiiueNDJMjYKaFyQ 提取码: fkpx 2.修改·vmoptions 1.Help" - ...

  8. cannot insert multiple commands into a prepared statement问题原因及解决办法

    问题是这样,我在对数据库进行写操作(添加.删除.修改)时,我想同时删除两个表中的两条关联数据,像这样 let sql = ` DELETE FROM bridge_parts WHERE id = $ ...

  9. 通过自己实现接口来加深理解SpringMVC的执行流程

    功能介绍 上篇文章[从源码角度了解SpringMVC的执行流程]通过接口源码向大家介绍了SpringMVC的执行流程,主要偏重于源码.这篇文件我们来自己实现那几个关键接口,来真实体验下SpringMV ...

  10. cogs 2. 旅行计划 dijkstra+打印路径小技巧

    2. 旅行计划 ★★   输入文件:djs.in   输出文件:djs.out   简单对比时间限制:3 s   内存限制:128 MB [题目描述] 过暑假了,阿杜准备出行旅游,他已经查到了某些城市 ...