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) ,右 ...
随机推荐
- 观察者模式(Observer)和发布(Publish/订阅模式(Subscribe)的区别
观察者模式(Observer)和发布(Publish/订阅模式(Subscribe)的区别 在翻阅资料的时候,有人把观察者(Observer)模式等同于发布(Publish)/订阅(Subscribe ...
- 2017-11-4—LTspice
LTspice这个软件基本上上手就能用,没什么好说的. ADI的一些器件下载的spice文件可能是.cir的需要使用cadence的pspice软件打开后另存为.dir文件使用.(比如ad4096) ...
- C# - 什么是事件绑定?
今天在学习C#时碰到了一个新词:“绑定事件”,不知道是什么东西? 请各位C#前辈指点!!!
- 关于ajax 返回值验证问题
如果后台返回布尔值true时 前端 if(data==true){ //true 不能加引号 否则就变成了字符串了 alert(data+'操作成功!'+status); }
- 201771010126.王燕《面向对象程序设计(Java)》第六周学习总结
实验六 继承定义与使用 实验时间 2018-9-28 1.实验目的与要求 (1) 理解继承的定义: 继承就是用已有类来构建新类的一种机制,当你继承了一个类时,就继承了这个类的方法和字段,同时你也可以在 ...
- 一种常见的maven打包后同名文件冲突错误
在使用一些开源框架的时候(比如spark.hadoop.lucene等),偶尔会见到说找不到某个具体实现类或者某个配置(比如spark的akka配置)不见了. 部分例子如下: [Lucene]An S ...
- [LeetCode] Random Point in Non-overlapping Rectangles 非重叠矩形中的随机点
Given a list of non-overlapping axis-aligned rectangles rects, write a function pick which randomly ...
- Windows下自带压缩文件工具之-makecab
在内网渗透时,当没有rar.7z等压缩工具时候,拖取文件的时候为了防止流量过大,又必须压缩把文件压缩.当然你可以自己上传一个压缩工具.Windows自带制作压缩文件工具makecb你可以了解哈.压缩单 ...
- jquery各版本
各版本下载: https://www.jb51.net/zt/jquerydown.htm
- Codeforces 1154F - Shovels Shop - [DP]
题目链接:https://codeforces.com/contest/1154/problem/F 题解: 首先,可以确定的是: 1.$(x,y)$ 里 $x>k$ 的都不可能用: 2.肯定买 ...