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


The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.

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

题目

给定元素不变的矩阵,求各种子矩阵和。

思路

Given matrix = [
[3, 0, 1, 4, 2],
[5, 6, 3, 2, 1],
[1, , 0, 1, 5],
[4, 1, 0, 1, 7],
[1, 0, 3, , 5]
] sumRegion(2, 1, 4, 3) -> 8
(2,1) 为黄色range左上角的坐标, 所在坐标对应的点为2
(4,3) 为黄色range右下角的坐标, 所在坐标对应的点为0
黄色range中 2 + 0 + 0 + 1 + 0 + 1 + 0 + 3 + 0 = 8 比如, input matrix为
     2    0    -3    4
6 3 2 -1
5 4 7 3
2 -6 8 1

多加一行一列方便写code,变成dp matrix为

 0    0    0     0    0
2 0 -3 4
6 3 2 -1
5 4 7 3
2 -6 8 1

开始fill dp matrix

dp[i][j]表示sum of rectangle from (0,0) to matrix (i-1, j-1)

 0    0    0     0    0
2 2 -1 3 //-> first row: easy to fill(累加)
 0    0    0     0    0
2 -1 3 0 15
// -> first col: easy to fill(累加)
 0    0    0     0    0
0 2 2 -1 3
0 8 X -> dp[i][j] = dp[i-1][j] // 正上方 2
0 13 + dp[i][j-1] // 正左方 8
0 15 + matrix [i-1][j-1] // input matrix 该位置值
- dp[i-1][j-1] // 左上角 2 ,重复加了两次需要减去一次

代码

 class NumMatrix {
private int[][] dp; /* 1.build and fill dp matrix in O(m*n) time */
public NumMatrix(int[][] matrix) {
int row = 0;
int col = 0;
if (matrix.length != 0) {
row = matrix.length;
col = matrix[0].length;
}
dp = new int[row + 1][col + 1];
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + matrix[i - 1][j - 1] - dp[i - 1][j - 1];
}
} } /*2. query in O(1) time */
public int sumRegion(int row1, int col1, int row2, int col2) {
/* coz dp matrix has size 1 greater one more than original matrix*/
row1++;
col1++;
row2++;
col2++;
return dp[row2][col2] - dp[row1 - 1][col2] - dp[row2][col1 - 1] + dp[row1 - 1][col1 - 1];
}
}

代码

 class NumMatrix {
private int[][] dp; /* 1.build and fill dp matrix in O(m*n) time */
public NumMatrix(int[][] matrix) {
int row = 0;
int col = 0;
if (matrix.length != 0) {
row = matrix.length;
col = matrix[0].length;
}
dp = new int[row + 1][col + 1];
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[0].length; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + matrix[i - 1][j - 1] - dp[i - 1][j - 1];
}
} } /*2. query in O(1) time */
public int sumRegion(int row1, int col1, int row2, int col2) {
/* coz dp matrix has size 1 greater one more than original matrix*/
row1++;
col1++;
row2++;
col2++;
return dp[row2][col2] - dp[row1 - 1][col2] - dp[row2][col1 - 1] + dp[row1 - 1][col1 - 1];
}
}

[leetcode]304. Range Sum Query 2D - Immutable二维区间求和 - 不变的更多相关文章

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

  2. LeetCode 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变(C++/Java)

    题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  3. 304 Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, col1) = (2, 1) ,右 ...

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

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

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

  8. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

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

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

随机推荐

  1. redis 学习笔记1(安装以及控制台命令)

    为什么要学习这个? 分布式技术必会,得益于redis的设计理念,内存数据库,epoll(多路复用)模型,单线程模型除去了锁和上下文切换,提高了性能.单线程保证执行顺序(轮询),在分布式环境下对于数据的 ...

  2. django403错误(转)

    原文:http://blog.sina.com.cn/s/blog_60ccc6e101011ku0.html 处理过程 1.按提示及google结果修改setting.py,在MIDDLEWARE_ ...

  3. Spark 调优(转)

    Spark 调优 返回原文英文原文:Tuning Spark Because of the in-memory nature of most Spark computations, Spark pro ...

  4. Zabbix实现自动发现端口并监控

    1.新建客户端需要的脚本 # vim discovertcpport.sh #!/bin/bash portarray=(`sudo netstat -tnlp|egrep -i "$1&q ...

  5. Zabbix邮件报警配置

    一.安装sendmail或者postfix yum install sendmail #安装 service sendmail start #启动 chkconfig sendmail on #设置开 ...

  6. linux 3.10 tcp的accept测试

    net.ipv4.tcp_abort_on_overflow 为 0 有个兄弟跟我说accept的时候,如果故意不去accept,那么客户端connect的时候,一开始很快,后来就很慢: connec ...

  7. sp_executesql 或者 EXECUTE 执行动态sql的权限问题

    当 sp_executesql 或 EXECUTE 语句执行字符串时,字符串将作为它的自包含批处理执行.SQL Server 会将字符串中的一个或多个 Transact-SQL 语句编译为独立于批处理 ...

  8. 常用dos命令和windows系统快捷键

    一.dos命令[重难点]1.OS——操作系统2.如何进入dos窗口?——P111 开始——程序——运行——输入cmd3.常用的dos操作命令 3.1 返回上一级 cd.. 3.2 返回到根目录 cd\ ...

  9. spring boot 访问jsp 弹出下载

    在pom.xml中加入以下jar 包 <!-- 引入Spring Boot 内嵌的Tomcat对jsp的解析包--><dependency> <groupId>or ...

  10. 吴裕雄 python 爬虫(1)

    from urllib.parse import urlparse url = 'http://www.pm25x.com/city/beijing.htm' o = urlparse(url) pr ...