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) ,右 ...
随机推荐
- ssh登录慢的解决办法
ubuntu的ssh登录有点慢,其实是很慢 google了一把,发现可以这样解决: (1)可能是DNS反向解析的问题 对于这样的问题,可以在/etc/ssh/sshd_config 中添加/修改: U ...
- Web前端学习攻略
HTML+CSS: HTML进阶.CSS进阶.div+css布局.HTML+css整站开发. JavaScript基础: Js基础教程.js内置对象常用方法.常见DOM树操作大全.ECMAscript ...
- 一文了解ConfigurationConditon接口
ConfigurationCondition 接口说明 @Conditional 和 Condition 在了解ConfigurationCondition 接口之前,先通过一个示例来了解一下@C ...
- Linux下文件名正常,下载之后在windows打开为乱码
说明:在Linux下编码为utf-8,在windows下位GBK 1. 2. 3. 4. 5. 6. 7. 8.
- insert小细节,大问题
今天现场报流程无法查看,已查看流程表中没有数据了.昨天有运行过删除垃圾数据的脚步.大致过程是: create table bak_test a as select * from test; creat ...
- win7激活附带激活软件
链接: https://pan.baidu.com/s/1i46yoHR 密码: 7k6y
- GS(道具,帮会)定时存储
//最近数据库存储做了重大改变,数据库内部的回头再说,先看看GS这边的 .现在感觉数据库的状态将请求包放入命令队列中,以前是全部放进去,这样让其他的数据库操作不会随着数据库定时器而变慢,GS线程去驱动 ...
- 函数模板&类模板
#include <iostream> #if 0//函数模板 template<typename T> T max(T a, T b, T c)//函数模板 { if (a ...
- 校验时间冲突SQL写法
校验时间是否有冲突,时间是就是看两个时间断是否有交集(写法有两种): a.SELECT * FROM xxx WHERE (startTime > a AND startTime < b) ...
- To discount or not to discount in reinforcement learning: A case study comparing R learning and Q learning
https://www.cs.cmu.edu/afs/cs/project/jair/pub/volume4/kaelbling96a-html/node26.html [平均-打折奖励] Schwa ...