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 ...
随机推荐
- php的socket通信【转载】
对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1. 什么是TCP/IP.UDP?2. Soc ...
- <a href="ip地址" target=""_blank">a里面的target</a>
HTML <a> 标签的 target 属性 定义和用法 <a> 标签的 target 属性规定在何处打开链接文档. 如果在一个 <a> 标签内包含一个 targe ...
- Swap Nodes in Pairs(链表操作)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- oracle删除表前先判断表是否存在
DECLARE numbe NUMBER;BEGIN SELECT COUNT(1) INTO numbe FROM USER_TABLES WHERE TABLE_NAME = ...
- JVM定位程序假死或cpu占用高的线程
linux系统: 参考:https://blog.csdn.net/qq_40197576/article/details/80287515 1>使用top命令查看占用cpu进程情况,得到jav ...
- mysql你确定掌握的那些sql语句
1.创建表 create table test(uid int not null,create_time timestamp default current_timestamp); 即:没有双引号,单 ...
- Python学习系列之异常处理
什么是异常处理 python内置了一套try···except···finally的错误处理机制 当程序出错的时候进行捕捉,然后根据捕捉到的错误信息进行响相应的处理 常用的内建异常 初识异常处理 如例 ...
- BUILD FAILED D:\build.xml:2: 前言中不同意有内容。
1.错误描写叙述 Microsoft Windows [版本号 6.1.7601] 版权全部 (c) 2009 Microsoft Corporation. 保留全部权利. C:\Users\Admi ...
- FaceBook开源库Fresco
讨论学习使用 关于 Fresco Fresco 是一个强大的图片载入组件. Fresco 中设计有一个叫做 image pipeline 的模块.它负责从网络.从本地文件系统.本地资源载入图片. 为了 ...
- ajax请求锁屏功能
我们有时候在进行ajax请求的时候希望页面不允许点击,等请求结束之后才可以进行点击,那么可以写: $(".cloudos-container").ajaxStart($.block ...