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. JZOJ 【NOIP2016提高A组集训第16场11.15】SJR的直线

    JZOJ [NOIP2016提高A组集训第16场11.15]SJR的直线 题目 Description Input Output Sample Input 6 0 1 0 -5 3 0 -5 -2 2 ...

  2. 初学者值得拥有Hadoop单机模式环境搭建

    单机模式Hadoop环境搭建 Hadoop环境搭建流程图 具体过程 文章目录 单机模式Hadoop环境搭建 Hadoop环境搭建流程图 具体过程 1.搭建准备工作 (1)关闭防火墙 (2)关闭seli ...

  3. 第7章 Python类型、类、协议目录

    第7.1节 面向对象程序设计的相关知识 第7.2节 关于面向对象设计的一些思考 第7.3节 Python特色的面向对象设计:协议.多态及鸭子类型 第7.4节 Python中与众不同的类 第7.5节 揭 ...

  4. crawlergo动态爬虫去除Spidername使用

    本来是想用AWVS的爬虫来联动Xray的,但是需要主机安装AWVS,再进行规则联动,只是使用其中的目标爬虫功能感觉就太重了,在github上面找到了由360 0Kee-Team团队从360天相中分离出 ...

  5. 大数据技术体系 && NoSQL数据库的基本原理

    1.NoSQL产生的原因 目前关系型数据库难以应对日益增多的海量数据,横向的分布式扩展能力比较弱,因此构建出非关系型数据库(所谓的NoSQL),其目的是为了构建一种结构简单.分布式.易扩展.效率高且使 ...

  6. MySQL存储引擎:MyISAM和InnoDB的区别

    MyISAM和InnoDB的区别 定义 InnoDB:MySQL默认的事务型引擎,也是最重要和使用最广泛的存储引擎.它被设计成为大量的短期事务,短期事务大部分情况下是正常提交的,很少被回滚.InnoD ...

  7. 将一个数组转化为需要的格式,来自react官网的商品列表示例

    //原来的格式 const PRODUCTS = [ { category: 'Sporting Goods', price: '$49.99', stocked: true, name: 'Foot ...

  8. AtCoder Regular Contest 108

    Contest Link Official Editorial A - Sum and Product Given are integers \(S\) and \(P\) . Is there a ...

  9. AtCoder Regular Contest 107(VP)

    Contest Link Official Editorial 比赛体验良好,网站全程没有挂.题面简洁好评,题目质量好评.对于我这个蒟蒻来说非常合适的一套题目. A. Simple Math Prob ...

  10. OpenWrt下基于OLSR的Ad-Hoc组网实现网络摄像头多节点访问

    文章目录 Ad-Hoc组网配置 摄像头端口映射 PC连接设置 结果 Ad-Hoc组网配置 参照博客 链接: link. 摄像头端口映射 这里使用到了海康网络摄像头,先将网络摄像头的网口连接到任意一个节 ...