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.

Sum(ABCD)=Sum(OD)−Sum(OB)−Sum(OC)+Sum(OA)

ps: 注意dp的递推公式

 class NumMatrix {
public:
vector<vector<int>> dp; NumMatrix(vector<vector<int>> matrix) {
int n =matrix.size();
int m = n==?:matrix[].size(); dp = vector<vector<int>>(n+,vector<int>(m+,));
for(int i = ;i<n;i++)
for(int j = ;j<m;j++){
dp[i+][j+] = dp[i][j+]+dp[i+][j]-dp[i][j]+matrix[i][j];
}
} int sumRegion(int rows1, int cols1, int rows2, int cols2) {
return dp[rows2+][cols2+] - dp[rows1][cols2+] - dp[rows2+][cols1] +dp[rows1][cols1];
}
}; /**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
 

304. Range Sum Query 2D - Immutable(动态规划)的更多相关文章

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

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

  3. 【LeetCode】304. Range Sum Query 2D - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...

  4. 304. Range Sum Query 2D - Immutable

    题目: 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 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. 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 ...

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

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

随机推荐

  1. DPI与DFI技术分析

    DPI全称为“Deep Packet Inspection”,称为“深度包检测”.DPI技术在分析包头的基础上,增加了对应用层的分析,是一种基于应用层的流量检测和控制技术,当IP数据包.TCP或UDP ...

  2. java 多态 向上 向下转型

    向上转型 将子类对象当作父类对象     子类对象------>父类对象 先实例化子类 父类 父类对象 = 子类实例 package test2; class Father{ public vo ...

  3. php curl 跨域情趣

    function curl_post($url='',$postdata='',$options=array()){ $ch=curl_init($url); curl_setopt($ch,CURL ...

  4. JAVA使用POI如何导出百万级别数据(转)

    https://blog.csdn.net/happyljw/article/details/52809244   用过POI的人都知道,在POI以前的版本中并不支持大数据量的处理,如果数据量过多还会 ...

  5. react中constructor()和super()的具体含义以及如何使用

    1.constructor()---super( )的基本含义 constructor()--构造方法 这是ES6对类的默认方法,通过new命令生成对象实例时自动调用该方法.并且,该方法是类中必须有的 ...

  6. (72)Wangdao.com第十二天_JavaScript 错误处理机制

    1. Error 实例对象 JavaScript 解析或运行时,一旦发生错误,引擎就会抛出一个错误对象. JavaScript 原生提供Error构造函数,所有抛出的错误都是这个构造函数的实例. va ...

  7. (86)Wangdao.com第十九天_JavaScript 接口之 ParentNode 和 ChildNode

    ParentNode 接口,ChildNode 接口 节点对象除了继承 Node 接口以外,还会继承其他接口. ParentNode 接口 表示当前节点是一个父节点,提供一些处理子节点的方法. Chi ...

  8. 黑盒测试实践——day03

    一.任务进展情况 目前基本确定选取的测试工具是Testwriter,测试的web系统还在待定状态,小组成员都在网上搜集相关知识,学习相关的测试技术.        二.存在的问题 Testwriter ...

  9. 输入正整数n,求各位数字和

    import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/5 10:24 * @description ...

  10. 01背包 || BZOJ 1606: [Usaco2008 Dec]Hay For Sale 购买干草 || Luogu P2925 [USACO08DEC]干草出售Hay For Sale

    题面:P2925 [USACO08DEC]干草出售Hay For Sale 题解:无 代码: #include<cstdio> #include<cstring> #inclu ...