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. 19_B门长时曝光APP

    知识很基础-- 前几天买了个单反,特别想拍B门长时间曝光的效果.后来想想不如自己写个APP,实现屏幕背景的随机颜色以及全屏显示文字. 先上图: 这两张图片的左侧都很亮,这是因为APP里面忘记把&quo ...

  2. AOV图与拓扑排序&AOE图与关键路径

    AOV网:所有的工程或者某种流程可以分为若干个小的工程或阶段,这些小的工程或阶段就称为活动.若以图中的顶点来表示活动,有向边表示活动之间的优先关系,则这样活动在顶点上的有向图称为AOV网. 拓扑排序算 ...

  3. 查询Oracle日志文件的方法

    Oracle日志文件相信经常使用Oracle数据库的朋友都比较熟悉了,下面将为您介绍的是查询Oracle日志文件的几种方法,供您参考学习. 1.查询系统使用的是哪一组日志文件: select * fr ...

  4. Java面试专题-多线程篇(2)- 锁和线程池

  5. 第8.9节 Python类中内置的查看直接父类的__bases__属性

    终于介绍完了__init__方法和__new__方法,接下来轻松一下,本节介绍类中内置的__bases__属性. 一. 语法释义 Python 为所有类都提供了一个 bases 属性,通过该属性可以查 ...

  6. 分布式文件系统HDFS-部署和配置

    1 部署HDFS HDFS的基本操作可以分为集群管理和文件系统操作两种类型: 集群管理:包括Namenodede 的格式化.集群的启动和停止.集群信息查看等. 文件系统:包括对目录.文件和权限等内容的 ...

  7. 对flask的学习

    任务需求:一个登录,注册页面 任务环境:pycharm 2018 专业版,python3.7,win 10专业版 ------------------------------------------- ...

  8. python 练习洗牌

    生成随机数需要引入random模块,学习下random模块中常用的几个函数: random.random() 用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.un ...

  9. 图论——迪杰斯特拉算法(Dijkstra)实现,leetcode

    迪杰斯特拉算法(Dijkstra):求一点到另外一点的最短距离 两种实现方法: 邻接矩阵,时间复杂度O(n^2) 邻接表+优先队列,时间复杂度O(mlogn)(适用于稀疏图) (n:图的节点数,m:图 ...

  10. 【科技】位运算(bitset)优化最长公共子序列算法

    最长公共子序列(LCS)问题 你有两个字符串 \(A,B\),字符集为 \(\Sigma\),求 \(A, B\) 的最长公共子序列. 简单动态规划 首先有一个广为人知的 dp:\(f_{i,j}\) ...