[leetcode]304Range Sum Query 2D - Immutable动态规划计算二维数组中子数组的sum
303一维数组的升级版,方法就是用二维数组res存下从(0,0)到当前位置的sum,存的方法是动态规划,看着二维数组画圈比较好搞清楚其中的加减法
算子数组的sum的时候也是和存差不多的逻辑,就是某一部分加上另一部分,然后减去某一部分,逻辑画画圈就能看出来
比价重要的是动态规划存数的过程,以后二维数组问题应该会经常用
package com.DynamicProgramming; import java.util.HashMap;
import java.util.Map; /**
* 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).
* 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.
*/
public class Q304RangeSumQuery2D_Immutable {
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
class NumMatrix {
int[][] data;
public NumMatrix(int[][] matrix) {
if (matrix.length == 0)
return;
int m = matrix.length;
int n = matrix[0].length;
data = new int[m][n];
//初始条件1
data[0][0] = matrix[0][0];
//初始条件2,同时也是动态规划1
for (int i = 1; i < m; i++) {
data[i][0] = data[i-1][0] + matrix[i][0];
}
for (int i = 1; i < n; i++) {
data[0][i] = data[0][i-1] + matrix[0][i];
}
//记录从当前位置到(0,0)点的sum,动态规划2
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
data[i][j] = data[i-1][j] + data[i][j-1] - data[i-1][j-1] + matrix[i][j];
}
}
} public int sumRegion(int row1, int col1, int row2, int col2) {
int res = data[row2][col2];
if (col1 >= 1 && row1 >= 1)
{
res = res - data[row2][col1-1] - data[row1-1][col2] + data[row1-1][col1-1];
}
else if (col1 >= 1)
res -= data[row2][col1-1];
else if (row1 >= 1)
res -= data[row1-1][col2];
return res;
}
} }
[leetcode]304Range Sum Query 2D - Immutable动态规划计算二维数组中子数组的sum的更多相关文章
- 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 - Immutable & Range Sum Query 2D - Immutable
Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indice ...
- 【刷题-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 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 ...
- 【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 OJ:Range Sum Query 2D - Immutable(区域和2D版本)
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
随机推荐
- 记一次容器CPU高占用问题排查
起因:发现docker中有两个容器的CPU持续在百分之95以上运行了一晚上 执行命令:docker stats 发现这个两个大兄弟一点没歇满负荷跑了一晚上,再这么下去怕不是要GG 容器里跑的是JAVA ...
- RedHat操作指令第2篇
六.RPM包管理命令 主要功能 查询RPM软件.包文件的相关信息 安装.升级.卸载RPM软件包 维护RPM数据库信息 查询RPM软件信息 查询已安装的RPM软件信息 格式:rpm -q[子选项] [软 ...
- CobaltStrike3.14&3.8安装&中文乱码解决
工具简介 Cobalt Strike 一款以 Metasploit 为基础的 GUI 框架式渗透测试工具,集成了端口转发.服务扫描,自动化溢出,多模式端口监听,exe.powershell 木马生成等 ...
- Python调用云服务器AWVS13API接口批量扫描(指哪打哪)
最近因为实习的原因,为了减少一部分的工作量,在阿里云服务器上搭建了AWVS扫描器 方便摸鱼 但是发现AWVS貌似没有批量添加的方法,作者只好把整理的URL.txt捏了又捏 手动输入是不可能手动输入的, ...
- 性能测试学习之路 (三)jmeter常见性能指标(相关术语、聚合报告 && 服务器性能监控配置 && 图形结果 && 概要报告)
1 性能测试目的 性能测试的目的:验证软件系统是否能够达到用户提出的性能指标,同时发现软件系统中存在的性能瓶颈,以优化软件. 最后起到优化系统的目的性能测试包括如下几个方面: 1.评估系统的能力:测试 ...
- 在Centos7下docker配置自动化环境镜像(python3.7+selenium 3.11+firefox 62+geckodriver 0.21)
最近在学习Docker,准备做自动化测试代码集成的功能.如下文章的前提是已经安装好linux系统,且成功安装好Docker. 接下来我会按步骤一步一步的对自动化需要的一些环境进行安装,如果没有特别说明 ...
- 百度网盘下载器:SpeedPan2.3.8
SpeedPan是款百度网盘资源下载工具,下载速度还行(至少比百度网盘快太多了),支持登录百度账号,也支持免登录下载.官网免费版的取消分享了,我从油管上看到了这个软件,分享给大家. 天翼云:https ...
- 美国SEC主席离任,Panda Global 前瞻数字资产监管政策变化
在上上个周末,也就是6月20号,美国的证券行业发生了一件值得反复回味的的事情--美国SEC现任主席Jay Clayton宣布即将离任,对于数字资产行业而言,未来监管政策将如何演变突然有了无限的遐想空间 ...
- AcWing 1194. 岛和桥
\(f[s][i][j]\) 表示一条有向路径(不经过重复点),当前路径点集合为 \(s\),最后两个点是 \(j\) → \(i\) 的最大价值 \(g[s][i][j]\) 类似,不过是方案数. ...
- Zabbix自定义模板监控多个url接口
一.脚本配置 1.监控脚本 /etc/zabbix/zabbix_agent2.d/scripts/web_site_code_status.sh #!/bin/bash url_discovery( ...