304. Range Sum Query 2D - Immutable(动态规划)

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.




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(动态规划)的更多相关文章
- 【刷题-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 ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...
- 304. Range Sum Query 2D - Immutable
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- [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 ...
- 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 二维区域和检索 - 矩阵不可变(C++/Java)
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- 304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...
随机推荐
- elasticsearch搜索框架的安装相关
安装JAVA SE 百度一下JAVA SE,按照自己的平台,位数选择就是了, 这里遇到过一个坑,双击exe安装包一直无法打开jdk的安装,在任务管理器里面就一闪而过, 后来我卸载了所有JAVA的相关安 ...
- 查看百度CUID方法
百度地图CUID号获取方法:安卓系统百度地图APP→“设置”→ 长按“关于”
- Do-Now—团队Scrum 冲刺博客二
各个成员今日完成的任务 侯泽洋:完成奖励页面设计,完成奖励从云端拉取到本地:完成奖励从云端拉取到本地 周亚杰:完成个人中心页面设计,登录界面美化:注册登录界面美化 王志伟:完成倒计时功能,并对页面进行 ...
- php实现多进程和关闭进程
一.php实现多进程 PHP有个pcntl_fork的函数可以实现多进程,但要加载pcntl拓展,而且只有在linux下才能编译这个拓展. 先代码: <?php$arr = ['30000000 ...
- JavaScript(七)
类型转换 1.直接转换 parseInt() 与 parseFloat() alert('12'+7); //弹出127 alert( parseInt('12') + 7 ); //弹出19 ale ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习6
#include <iostream>#include <string>using namespace std;struct car{ string pro; int yea ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习4
#include <iostream>using namespace std;const MAXSIZE=12;int main(){ char *month[MAXSIZE]={&quo ...
- java列表数据基本操作
列表数据组基本的增删改查 package cn.edu.fhj.day002; import java.util.ArrayList; public class ArrList { // 定义一个主函 ...
- jquery提示sucess
这是学习笔记. 今天做东西的时候,想把体验做好,于是打算再ajax success字段中添加函数实现提示sucess. 用了jquery的fadeIn 跟fadeOut,再fadeIn的callbac ...
- Elastic 之倒排索引(二)
常规索引建立: 文档-->关键词的映射过程(正向索引) 缺点:费时 便利全部文档 倒排反向建立索引: 关键词-->文档的映射 反向到倒排索引:将索引的关键词出现的文档的位置和出现频率通过文 ...