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的更多相关文章

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

  2. [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 ...

  3. 【刷题-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 ...

  4. [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 ...

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

  6. 【LeetCode】304. Range Sum Query 2D - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...

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

  8. [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 ...

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

随机推荐

  1. OD断点操作

    原文链接:https://www.cnblogs.com/qiyeboy/p/6815988.html 在做Windows平台软件逆向时,Ollydbg是极其常用的逆向工具,动态调试功能非常强大.在调 ...

  2. pthread 互斥量

    参考文献: [1] https://computing.llnl.gov/tutorials/pthreads/ 温故知新 在 OS 中,每个进程都独立地拥有: Process ID, process ...

  3. Codeforces Round 665 (div2)

    2020.8.22 装修完了我的博客,喜欢这个造型,挂上友链就更好了 昨天cf就是一个彻头彻尾的悲剧,本来能上蓝,结果因为在A题耽误时间过多导致掉了30分,不过没关系,这算是一个小波动吧,影响不了什么 ...

  4. 第14.6节 使用Python urllib.request模拟浏览器访问网页的实现代码

    Python要访问一个网页并读取网页内容非常简单,在利用<第14.5节 利用浏览器获取的http信息构造Python网页访问的http请求头>的方法构建了请求http报文的请求头情况下,使 ...

  5. PyQt(Python+Qt)学习随笔:QAbstractItemView的iconSize属性

    老猿Python博文目录 老猿Python博客地址 视图的iconSize属性用于控制显示icon的项上的icon图标大小,在视图可见情况下设置该属性会导致视图上的显示项重新调整布局. 可以使用ico ...

  6. linux常用快捷键总结

    启动器:<super> 显示桌面:<super>D 文件管理器:<super>E 显示工作区:<super>S 打开终端:ctrl+alt+T 关闭窗口 ...

  7. john破解kali密码

    实验环境:kali 实验工具:john 所用命令: 1.查看/etc目录下的shadow文档,此文档记录了所有用户的用户名及密码hash值 2.使用命令echo 用户名:密码 > shadow, ...

  8. 小程序image无法显示图片

    图片路径中有中文 图片地址为http开头,图片只能在调试模式中显示,真机也必须开调试. 图片名称有空格 图片的后缀必须为小写的.png或者.jpg

  9. vue 编程式导航

    // 命名的路由(这里的name为路由中定义的name名称) this.$router.push({ name: 'user', params: { userId: '123' }}) // 带查询参 ...

  10. uniapp图片转base64

    直接上代码了,网上也很多一样的,这里记录下,因为仅仅第二种在真机微信小程序上我这里测试转换失败,所以就一并写在这里了: //图片转base64 urlTobase64(url){ // #ifdef ...