[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 ...
随机推荐
- 3D网页小实验——将txt配置文本转化为3D陈列室
设计目标:借鉴前辈编程者的经验将简单的配置文本转化为3D场景,并根据配置文件在场景中加入图片和可播放的视频,最终形成可浏览的3D陈列室. 一.使用效果 1.txt配置文件: (博客园的富文本编辑器会改 ...
- 20200311_最新爬取mzitu
废话不多, 直接上代码, python3.6: import requests from bs4 import BeautifulSoup import os import time; import ...
- Hadoop完全分布式模式安装部署
在Linux上搭建Hadoop系列:1.Hadoop环境搭建流程图2.搭建Hadoop单机模式3.搭建Hadoop伪分布式模式4.搭建Hadoop完全分布式模式 注:此教程皆是以范例讲述的,当然你可以 ...
- charles 常用功能(七)简易接口压力测试(repeat advance 功能)
接口请求次数.并发量.请求延迟时间均可配置 1.选中需要进行测试的接口,鼠标右键 选中[repeat advance] 设置迭代数量
- 利用canvas阴影功能与双线技巧绘制轨道交通大屏项目效果
利用canvas阴影功能与双线技巧绘制轨道交通大屏项目效果 前言 近日公司接到一个轨道系统的需求,需要将地铁线路及列车实时位置展示在大屏上.既然是大屏项目,那视觉效果当然是第一重点,咱们可以先来看看项 ...
- moviepy音视频剪辑:AudioClip的max_volume方法报TypeError: bad operand type for abs(): ‘list‘错
☞ ░ 前往老猿Python博文目录 ░ 一.环境 操作系统:win7 64位 moviepy:1.0.3 numpy:1.19.0 Python:3.7.2 二.应用代码及报错信息 应用代码 imp ...
- KM 算法
KM 算法 可能需要先去学学匈牙利算法等二分图相关知识. 模板题-洛谷P6577 [模板]二分图最大权完美匹配 给 \(n\) 和 \(m\) 与边 \(u_i,v_i,w_i(1\le i\le m ...
- 20201214-1 Json与pickle数据序列化
1-1 文件处理时,只能存字符串或者二进制 info = { 'name':'alex', 'age':22 } f = open("test.text", "w&quo ...
- MySQL 锁(完整版)
目录 锁总览 锁的作用 加锁流程 锁对数据库的影响 锁等待 死锁 锁类型 锁范围 锁方式 全局锁 全局读锁 全局QC锁 QC锁存在的问题: 备份锁 backup lock MDL锁 MDL锁类型 MD ...
- postgresql 运行sql文件
方法一: [postgres@node01 ~]$ psql -Upostgres postgres=# \l List of databases Name | Owner | Encoding | ...