304. Range Sum Query 2D - Immutable(动态规划)

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.




Sum(ABCD)=Sum(OD)−Sum(OB)−Sum(OC)+Sum(OA)
ps: 注意dp的递推公式
class NumMatrix {
public:
vector<vector<int>> dp;
NumMatrix(vector<vector<int>> matrix) {
int n =matrix.size();
int m = n==?:matrix[].size();
dp = vector<vector<int>>(n+,vector<int>(m+,));
for(int i = ;i<n;i++)
for(int j = ;j<m;j++){
dp[i+][j+] = dp[i][j+]+dp[i+][j]-dp[i][j]+matrix[i][j];
}
}
int sumRegion(int rows1, int cols1, int rows2, int cols2) {
return dp[rows2+][cols2+] - dp[rows1][cols2+] - dp[rows2+][cols1] +dp[rows1][cols1];
}
};
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
304. Range Sum Query 2D - Immutable(动态规划)的更多相关文章
- 【刷题-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 二维区域和检索 - 不可变
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 ...
- 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 ...
- 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 ...
- 304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...
随机推荐
- event.target 和 event.currentTarget 的区别
event.target This property of event objects is the object the event was dispatched on. It is differe ...
- Linux命令行参数前加--,-和不加杠
参数前“-”的表明后面的参数是字符形式. 参数前“--”的则表明后面的参数是单词形式. 参数前有横的是System V风格. 参数前没有横的是BSD风格.
- SSH(Spring Struts2 Hibernate)框架整合(xml版)
案例描述:使用SSH整合框架实现部门的添加功能 工程: Maven 数据库:Oracle 案例架构: 1.依赖jar包pom.xml <project xmlns="http://ma ...
- python#读csv,excel,json数据
#读csv,excel,json数据 with open('E:\\test\\xdd.csv','r') as f: for line in f.readlines(): print(line) i ...
- php的Memcached模块扩展
Memcached模块介绍 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态. ...
- swust oj 983
利用二叉树中序及后序遍历确定该二叉树的先序序列 1000(ms) 10000(kb) 2606 / 4908 已知二叉树的中序和先序遍历可以唯一确定后序遍历.已知中序和后序遍历可以唯一确定先序遍历,但 ...
- [math] 什么是双曲函数(转发)
我完全不记得上高中的时候学习过双曲函数...额,暴露了... 原文地址:https://zhuanlan.zhihu.com/p/20042215 可能是最好的讲解双曲函数的文章 零.写在前面 (近期 ...
- [dev][python] 从python2进阶到python3你都需要了解什么
基于python2快速掌握python3 0. 前言 这是一篇road map. 如果你会python2,读完这篇文章之后,你将掌握python3 1. 为什么会出现python3 Why Pytho ...
- COMP9021 PRINCIPLES OF PROGRAMMING
QUIZ 7COMP9021 PRINCIPLES OF PROGRAMMING$ python3 quiz_7.pyEnter four integers: 0 2 2 8Here is the g ...
- 《Redis 使用规范》
一:Redis 概述 - Redis 是内存级别的数据库,在一台普通电脑上,Redis 3.X 便可以读取 10 万个键值对(现在的Redis官方版本已经更新到了5.X,性能会更好). 二:关于Red ...