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).

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.
题解:
二维的情况和一维的思路类似,就是求一个递推式然后把和预处理出来,然后O(1)查询。
一维的递推式是:s[i]=s[i-1]+a[i];
二维的由画图可知:s[i][j]=a[i][j]+s[i-1][j]+s[i][j-1]-s[i-1][j-1];

class NumMatrix {
public:
NumMatrix(vector<vector<int>> &matrix) {
n=matrix.size();
m=n>?matrix[].size():;
s=vector<vector<int>>(n+,vector<int>(m+,));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
s[i][j]=matrix[i-][j-]+s[i-][j]+s[i][j-]-s[i-][j-];
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
return s[row2+][col2+]-s[row2+][col1]-s[row1][col2+]+s[row1][col1];
}
private:
int n,m;
vector<vector<int> >s;
};
// Your NumMatrix object will be instantiated and called as such:
// NumMatrix numMatrix(matrix);
// numMatrix.sumRegion(0, 1, 2, 3);
// numMatrix.sumRegion(1, 2, 3, 4);
leetcode 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]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 ...
- 【刷题-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 ...
- 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 ...
- 304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...
随机推荐
- .netMVC:Web页面向后台提交数据的方式和选择
众所周知Web前端页面主要由HTML/CSS/Javascript组成,当要通过与用户的交互实现各种功能时,就需要向后台提交一些数据或者操作.在Web世界里各种实现眼花缭乱,但究其根本,不外乎三种方式 ...
- js中比較好的继承方式
前面说到了原型和原型链,今天就来说说在面向对象中比較好的继承方式吧.先来看看两种基础的继承方式: 一.构造函数型 function People(name) { this.name=name; } P ...
- android studio 更新Gradle版本号方法
在导入其它项目时,常常因为gradle版本号不一致而导致不能编译 解决方法: 第一步: 按提示点击让它下载.事实上目的并非要它下载.因为这样速度会非常慢.这样做仅仅是为了让它在本地创建相应的文件夹结构 ...
- android 关于ScrollView 的博客做记录学习
1.Android ScrollView向上滑动控件顶部悬浮效果实现 2.[android]仿知乎ScrollView滚动改变标题栏透明度 3.github开源Android组件资源整理(五)Scro ...
- MongoDB 基本操作具体解释
MongoDB 最大的特点是他支持的查询语言很强大,其语法有点类似于面向对象的查询语 言.差点儿能够实现类似关系数据库单表查询的绝大部分功能,并且还支持对数据建立索引. 最后因为 MongoDB 能够 ...
- python编程基础:《http://www.cnblogs.com/wiki-royzhang/category/466416.html》
windows自动化 http://www.cnblogs.com/wiki-royzhang/category/466416.html
- Python --- Scrapy 命令(转)
Scrapy 命令 分为两种: 全局命令 和 项目命令 . 全局命令:在哪里都能使用. 项目命令:必须在爬虫项目里面才能使用. 全局命令 C:\Users\AOBO>scrapy -h Scra ...
- SQLServer中游标实例介绍(转)
引言 我们先不讲游标的什么概念,步骤及语法,先来看一个例子: 表一 OriginSalary 表二 AddSalary 现在有2张表,一张是OriginSal ...
- IE浏览器的判断
function compatibleIE8(){ var browser = navigator.appName; var b_version = navigator.appVersion; if( ...
- zendstudio 13.0
官网原版下载 http://downloads.zend.com/studio-eclipse/13.0.0/ZendStudio-13.0.0-win32.win32.x86.exe 破解补丁: 链 ...