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 ...
随机推荐
- NET Core 2.1.0 now available
ASP.NET Core 2.1.0 now available https://blogs.msdn.microsoft.com/webdev/2018/05/30/asp-net-core-2-1 ...
- (转)useradd用户,组管理案例
原文:https://www.cnblogs.com/Csir/p/6403830.html?utm_source=itdadao&utm_medium=referral 示例:要求oldbo ...
- Error: EPERM: operation not permitted,
转载自:https://blog.csdn.net/dong923700243/article/details/78989332 npm ERR! path E:\React\ReactNativeP ...
- 把一个HashMap的值全部取出来,放到两个数组中
先是从数据库中获取所有的值,返回一个HashMap类型的数据: <pre name="code" class="java"> private Has ...
- css继承性
不可继承的:display.margin.border.padding.background.height.min-height.max- height.width.min-width.max-wid ...
- php分页代码及总结
代码部分: <?PHPheader("Content-type:text/html;charset=utf-8");$pageSize = 10;//接收传入的分页码$pag ...
- JavaScript笔记6-数组新方法
七.ECMAScript5关于数组的新方法 1.forEach():遍历数组,并为每个元素调用传入的函数; 举例: var a = [1,2,3]; var sum = 0; //传一个 ...
- 【extjs6学习笔记】1.2 初始:MVC MVVM
模型 这表示数据层.该模型可以包含数据验证和逻辑来保持数据.在 ext js 中, 大多数模型都与一个数据存储一起使用. 视图 这表示用户界面. 是用户在屏幕上看到的组件. 在每次互动的用户与应用程序 ...
- python3操作mysql数据库表01(封装查询单条、多条数据)
#!/usr/bin/env python# -*- coding:UTF-8 -*- import pymysql# import os'''封装查询单条.多条数据'''# os.environ[' ...
- mysql-新增、更新、删除语句
1.插入数据: INSERT INTO t_book VALUES(NULL,'我爱我家',20,'张三',1); INSERT INTO t_book(id,bookName,price,autho ...