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 left corner (row1, col1) and lower right corner (row2, col2).
Range Sum Query 2D
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.
分析
思想与上一题相同。
注意下标处理方式。
AC代码
class NumMatrix {
public:
NumMatrix(vector<vector<int>> &matrix) {
if (matrix.empty())
return;
//求得二维矩阵的行列
int m = matrix.size(), n = matrix[0].size();
sums = vector<vector<int>>(m+1, vector<int>(n+1, 0));
sums[0][0] = matrix[0][0];
for (int j = 1; j <= n; ++j)
{
sums[0][j] = 0;
}
for (int i = 1; i <= m; ++i)
{
sums[i][0] = 0;
}
//
for (int i = 1; i <= m; ++i)
{
for (int j = 1; j <= n; ++j)
{
sums[i][j] = sums[i][j - 1] + sums[i - 1][j] - sums[i - 1][j - 1] + matrix[i-1][j-1];
}//for
}//for
}
int sumRegion(int row1, int col1, int row2, int col2) {
//求得二维矩阵的行列
int m = sums.size(), n = sums[0].size();
if (row1 < 0 || row1 >= m || col1 < 0 || col1 >= n || row2<0 || row2 >= m ||
col2<0 || col2 >= n || row1 >row2 || col1 > col2)
{
return 0;
}
return sums[row2+1][col2+1] - sums[row2+1][col1] - sums[row1][col2+1] + sums[row1][col1];
}
private:
vector<vector<int>> sums;
};
LeetCode(304)Range Sum Query 2D - Immutable的更多相关文章
- LeetCode(307) Range Sum Query - Mutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
- LeetCode(303)Range Sum Query - Immutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
- 【刷题-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] 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 ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...
- [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
Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fac ...
- [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 ...
随机推荐
- CentOS,net core2 sdk nginx、supervisor、mysql
CentOS下 .net core2 sdk nginx.supervisor.mysql环境搭建 作为.neter,看到.net core 2.0的正式发布,心里是有点小激动的,迫不及待的体验了一把 ...
- AXURE-手把手教你做汉化
我们默认下载的AXURE是英文版的,对于英文能力不足或者不习惯英文界面的,那必须使用汉化手段,网上也有很多朋友已经为大家做好了汉化文件,这里介绍一下如何自己做AXURE的汉化. 如何开始汉化 如何 ...
- Coroutine(协程)模式与线程
概念 协程(Coroutine)这个概念最早是Melvin Conway在1963年提出的,是并发运算中的概念,指两个子过程通过相互协作完成某个任务,用它可以实现协作式多任务,协程(coroutine ...
- C#实现程序单例日志输出
对于一个完整的程序系统,一个日志记录是必不可少的.可以用它来记录程序在运行过程中的运行状态和报错信息.比如,那些不想通过弹框提示的错误,程序执行过程中捕获的异常等. 首先,在你的解决方案中,适当的目录 ...
- springboot+shiro+cas实现单点登录之shiro端搭建
github:https://github.com/peterowang/shiro-cas 本文如有配置问题,请查看之前的springboot集成shiro的文章 1.配置ehcache缓存,在re ...
- 只用jsp实现同样的Servlet功能
Jsp最终都会转化成java形式的Servlet执行,因此也可以说Jsp的本质就是Servlet,在jsp执行后,会在服务器上(例如tomcat中)生成.java以及.class文件.具体执行过程如下 ...
- 关于Servlet中的转发和重定项
一:转发 首先转发属于服务器内部行为,通过浏览器的地址栏是看不到URL变化的.比如说客户端发送一个请求到ServletA,ServletA接收到请求,但是没有能力处理,但是ServletA知道Serv ...
- ABAP EXCEPTION
CX_ROOT | |--CX_STATIC_CHECK | |--CX_DYNAMIC_CHECK | | | |--CX_SY_ARITHMETIC_ERROR //运算 '&OPERAT ...
- JFinal视频教程-JFnal学院分享课
最近JFinal学院出了JFinal视频教程分享课,请笑纳~ 课程列表: 1.[JFinal版]微信小程序富文本渲染解决方案-html2wxml4J分享课 这个课程主要讲的是使用基于JFinal开发的 ...
- for...in、for...of、forEach()有什么区别
本文原链接:https://cloud.tencent.com/developer/article/1360074 for of 和 for in 循环 循环遍历数组的时候,你还在用 for 语句走天 ...