304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。
上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为8。
示例:
给定 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
注意:
你可以假设矩阵不可变。
会多次调用 sumRegion方法。
你可以假设 row1 ≤ row2 且 col1 ≤ col2。
C++:
class NumMatrix {
public:
NumMatrix(vector<vector<int>> matrix) {
if(matrix.empty()||matrix[0].empty())
{
return;
}
dp.resize(matrix.size()+1,vector<int>(matrix[0].size()+1,0));
for(int i=1;i<=matrix.size();++i)
{
for(int j=1;j<=matrix[0].size();++j)
{
dp[i][j]=dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1]+matrix[i-1][j-1];
}
}
} int sumRegion(int row1, int col1, int row2, int col2) {
return dp[row2+1][col2+1]-dp[row1][col2+1]-dp[row2+1][col1]+dp[row1][col1];
}
private:
vector<vector<int>> dp;
}; /**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
参考:https://www.cnblogs.com/grandyang/p/4958789.html
304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变的更多相关文章
- [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] 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 ...
- [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 ...
- [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
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 解题报告(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 ...
随机推荐
- HDU.P1100 Trees Made to Order 解题报告
http://www.cnblogs.com/keam37/p/3637717.html keam所有 转载请注明出处 Problem Description We can number binar ...
- SAP EP 设置Portal别名安全模式
Securing the Portal Alias Cookie Context We recommend that you set the portal alias cookie to be del ...
- java.lang.RuntimeException: JPedal Trial has now expired
具体提示: java.lang.RuntimeException: JPedal Trial has now expired jpedal-server-trial.jar jar包过期了,jpeda ...
- ASPNET Core 部署 Linux — 使用 Jexus Web Server
第一步 安装.Net Core环境 安装 dotnet 环境参见官方网站 https://www.microsoft.com/net/core. 选择对应的系统版本进行安装.安装完成过后 输入命令查看 ...
- 【python】一些好的学习网址
http://www.cnblogs.com/BeginMan/p/3179302.html http://www.cnblogs.com/huxi/category/251137.html http ...
- android的ndk学习(1)
android的ndk学习(1) 之前学了一段时间ndk,总认为要总结一下.ndk使得很方便地实现java和C与C++代码的相互沟通.合理地掌握使用ndk能够提高应用程序的运行效率.所以对于学习a ...
- Broccoli & Babel使用演示样例
1 创建项目project文件夹:test 2 在test下运行 npm init 按提示填写package.json文件 3 安装broccoli命令行工具broccoli-cli npm inst ...
- libsqlite3.dylib与libsqlite3.0.dylib的差别
在我们加入数据库框架时,在搜索框中输入sqlitekeyword,以下列表区会显示libsqlite3.dylib,libsqlite3.0.dylib. 此时我们选择libsqlite3.0.dyl ...
- 介绍Android拍照,录像开发的相关东东
Android下相机有自带的照片功能,可是作为开发人员,我们需要更为深层次的知道,怎么用,以及相关原理,这里我就这方面的学习,写一下心得,供博友参考. 第一种:调用系统自带相机界面. 这时我们在布局文 ...
- 【codevs2011】【LNOI2013】最小距离之和
floyed水题 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstr ...