题目:

给定一个二维矩阵 matrix,以下类型的多个请求:

计算其子矩形范围内元素的总和,该子矩阵的 左上角 为 (row1, col1) ,右下角 为 (row2, col2) 。
实现 NumMatrix 类:

NumMatrix(int[][] matrix) 给定整数矩阵 matrix 进行初始化
int sumRegion(int row1, int col1, int row2, int col2) 返回 左上角 (row1, col1) 、右下角 (row2, col2) 所描述的子矩阵的元素 总和

输入:
["NumMatrix","sumRegion","sumRegion","sumRegion"]
[[[[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]]],[2,1,4,3],[1,1,2,2],[1,2,2,4]]
输出:
[null, 8, 11, 12]

解释:
NumMatrix numMatrix = new NumMatrix([[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]]);
numMatrix.sumRegion(2, 1, 4, 3); // return 8 (红色矩形框的元素总和)
numMatrix.sumRegion(1, 1, 2, 2); // return 11 (绿色矩形框的元素总和)
numMatrix.sumRegion(1, 2, 2, 4); // return 12 (蓝色矩形框的元素总和)

提示:

m == matrix.length
n == matrix[i].length
1 <= m, n <= 200
-105 <= matrix[i][j] <= 105
0 <= row1 <= row2 < m
0 <= col1 <= col2 < n
最多调用 104 次 sumRegion 方法

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/range-sum-query-2d-immutable
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题思路:

1.先求presum[i][j]:从[0][0]到[i][j]位置的子矩阵所有元素之和;

2.再利用presum求子矩阵的面积。

 代码:

注意: presum 矩阵要比原矩阵多一行一列,是为了让第 0 行与第 0 列的元素也能使用上面的递推公式。如果 preSum 矩阵大小和 martix 大小相等,则需要对第 0 行与第 0 列特殊判断。所以求前缀和时从 1 开始,类似于一维前缀和,也是在比原数组多一个元素,主要是为了 presum[0] 时也能直接表示,不然需要特殊判断。

 1 class NumMatrix {
2 int[][] presum;
3
4 public NumMatrix(int[][] matrix) {
5 presum = new int[matrix.length + 1][matrix[0].length + 1];
6 for(int i = 0; i < matrix.length; i++){
7 for(int j = 0; j < matrix[0].length; j++){
8 presum[i + 1][j + 1] = presum[i + 1][j] + presum[i][j + 1] - presum[i][j] + matrix[i][j];
9 }
10 }
11 }
12 public int sumRegion(int row1, int col1, int row2, int col2) {
13 return presum[row2 + 1][col2 + 1] - presum[row2 + 1][col1] -presum[row1][col2 + 1] + presum[row1][col1];
14 }
15 }
16
17 /**
18 * Your NumMatrix object will be instantiated and called as such:
19 * NumMatrix obj = new NumMatrix(matrix);
20 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
21 */

力扣304(java)-二维区域和检索-矩阵不可变(中等)的更多相关文章

  1. Java实现 LeetCode 304 二维区域和检索 - 矩阵不可变

    304. 二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). Range Sum Qu ...

  2. Leetcode 304.二维区域和检索-矩阵不可变

    二维区域和检索 - 矩阵不可变 给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2). 上图子矩阵左上角 (row1, c ...

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

  4. [Swift]LeetCode304. 二维区域和检索 - 矩阵不可变 | 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]303.区域和检索&&304.二维区域和检索

    题目 1.区域和检索: 简单题,前缀和方法 乍一看就觉得应该用前缀和来做,一个数组多次查询. 实现方法: 新建一个private数组prefix_sum[i],用来存储nums前i个数组的和, 需要找 ...

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

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

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

  9. 领扣(LeetCode)二维区域和检索 个人题解

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

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

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

随机推荐

  1. react start 后 url 后面不带/ 解决思路

    > navigator@0.1.0 dev H:\2020home\giteez\navigator > node scripts/start.js Compiled successful ...

  2. 软件推荐 Notable / 现改用 Vnote 了

    https://notable.app/#download

  3. k8s通过help、dry-run、explain提高编写yaml效率

    在Kubernetes(k8s)环境中,help.dry-run和explain命令可以帮助你提高编写YAML文件的效率.这些命令提供了关于资源定义.命令用法和字段说明的信息,从而让你能够更快速.更准 ...

  4. 文旅新体验!3DCAT助力广州非遗“元宇宙”街区炫酷亮相

    2022年6月12日,2022年"文化和自然遗产日"广州非遗宣传展示主会场暨广州非遗街区(北京路)开街仪式在南越王博物院(王宫展区)举行. 本次活动由广州市文化广电旅游局主办,广州 ...

  5. Sealos 云开发:Laf 出嫁了,与 Sealos 正式结合!

    千呼万唤始出来,Laf 云开发最近已正式与 Sealos 融合,入住 Sealos!大家可以登录 Sealos 公有云 体验和使用,现在正式介绍一下 Sealos 云开发. Sealos 云开发是什么 ...

  6. 常用命令--htpasswd--(网站加密)

    常用命令htpasswd(网站加密) 常用选项 htpasswd 是一个用于创建和管理HTTP基本认证密码文件的命令行工具,通常与Apache Web服务器一起使用.以下是 htpasswd 常用选项 ...

  7. multisim的支路及总线设计

    Multisim的支路及总线设计 1.实验原理 最近在使用multisim设计时,用到了总线和支路设计,这里记录一下,方便以后查阅相关操作.其中主要是总线的使用和支路连接器的使用. 2.实验操作 (1 ...

  8. 连接Windows 平台 KingbaseES异常

    概述 应用连接Windows平台的KingbaseES 数据库,报错"com.kingbase8.util.KSQLException: 致命错误: 用户"system" ...

  9. #模型转换#洛谷 6075 [JSOI2015]子集选取

    题目 分析 \(n\)个元素可以独立操作,考虑单个元素, 则选不选择一定有一道分界线, 而这条分界线正好要走\(k\)次, 每次可以选择向上走或向右走,所以为\(2^k\), 由于\(n\)个元素相互 ...

  10. #折半搜索,状压dp#nssl 1471 Y

    分析 设\(dp[i][j][s]\)表示从\(i\)到\(j\)的一条路径状态为\(s\)是否存在 但是这样肯定会T掉,考虑拼凑路径,分成两部分, 设\(dp[0/1][s]\)分别表示以某个起点/ ...