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.

这道题让我们求一个二维区域和的检索,是之前那道题Range Sum Query - Immutable 区域和检索的延伸。有了之前那道题的基础,我们知道这道题其实也是换汤不换药,还是要建立一个累计区域和的数组,然后根据边界值的加减法来快速求出给定区域之和。这里我们维护一个二维数组dp,其中dp[i][j]表示累计区间(0, 0)到(i, j)这个矩形区间所有的数字之和,那么此时如果我们想要快速求出(r1, c1)到(r2, c2)的矩形区间时,只需dp[r2][c2] - dp[r2][c1 - 1] - dp[r1 - 1][c2] + dp[r1 - 1][c1 - 1]即可,下面的代码中我们由于用了辅助列和辅助行,所以下标会有些变化,参见代码如下:

class NumMatrix {
public:
NumMatrix(vector<vector<int> > &matrix) {
if (matrix.empty() || matrix[].empty()) return;
dp.resize(matrix.size() + , vector<int>(matrix[].size() + , ));
for (int i = ; i <= matrix.size(); ++i) {
for (int j = ; j <= matrix[].size(); ++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[row1][col2 + ] - dp[row2 + ][col1] + dp[row1][col1];
} private:
vector<vector<int> > dp;
};

类似题目:

Range Sum Query 2D - Mutable

Range Sum Query - Immutable

Range Sum Query - Mutable

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变的更多相关文章

  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 - Mutable 二维区域和检索 - 可变

    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 二维区域和检索 - 矩阵不可变(C++/Java)

    题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

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

  6. [LeetCode] Range Sum Query 2D - Immutable

    Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fac ...

  7. [LeetCode] Range Sum Query - Immutable & Range Sum Query 2D - Immutable

    Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...

  8. 【刷题-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 ...

  9. Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

随机推荐

  1. scikit-learn一般实例之七:使用多输出评估器进行人脸完成

    本例将展示使用多输出评估期来实现图像完成.目标是根据给出的上半部分人脸预测人脸的下半部分. 第一列展示的是真实的人脸,接下来的列分别展示了随机森林,K近邻,线性回归和岭回归对人脸下半部分的预测. # ...

  2. 供应链需求调研CheckList

    总体(General) 基本情况 1.    企业地址.邮编.电话.传真,项目联系人等基本资料. 2.    企业经营范围,产品线和主导产品. 3.    企业近几年的产值及销售额. 4.    企业 ...

  3. ES5概述(ECMAScript262/5.1)

    ES5概述(ECMAScript262/5.1) 本文参考:ES5概述 ECMAScript 是基于对象的: 基本语言和宿主设施都由对象提供,ECMAScript 程序是一组可通信的对象.ECMASc ...

  4. arcgis api for js入门开发系列二不同地图服务展示(含源代码)

    上一篇介绍了arcgis api离线部署,这篇开始正式介绍arcgis api for js开发:想要学习webgis开发,首先得熟悉了解前端技术,比如界面布局设计的html+css,核心的是java ...

  5. Java实现约瑟夫环

    什么是约瑟夫环呢? 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个 ...

  6. ip命令和ifconfig命令(转载)

    Linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者.使用ip命令,只需一个命令,你就能很轻松地执行一些网络管理任务.ifconfig是net-tools中已被废弃使用的一个命 ...

  7. JVM之上的语言小集

    1 JVM上的编程语言https://en.wikipedia.org/wiki/List_of_JVM_languages主要的有:Clojure, a functional Lisp dialec ...

  8. Android 面试题--Activity

    1.什么是 Activity?Activity是Android组件中最基本也是最为常见用的四大组件(Activity,Service服务,Content Provider内容提供,BroadcastR ...

  9. android JNI 调用NDK方法

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  10. django ftp 研究

    做个网络运维多年,一直在用filezilla搭建ftp服务器,总体上还是不错的.但是用户配置与管理方面还是不太方便,希望能够通过web页面对用户进行管理和对ftp服务器进行监控. 工具: (1)dja ...