题目:

给定一个二维矩阵 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. WPF之模板

    目录 模板的内涵 数据的外衣DataTemplate UserControl例子 DataTemplate例子 控件的外衣 ControlTemplate 解剖控件 ItemsControl的Pane ...

  2. Review Book for GEE(Graduate Entrance Examination)

    English is made up of phrases and idioms, in the case of both written and spoken usage. When learnin ...

  3. 基于ADS1292芯片的解决方案之源码解析

    接口解析  A 该芯片和主控使用的是SPI接口通信的. SPI接口一般有四根线,确保四根线准确连接是对的. B 该芯片可以有中断模式数据触发,所以,主控mcu需要有外部中断处理流程. //DRDY中断 ...

  4. Android Swtich开关样式调整

    原文:Android Swtich开关样式调整 - Stars-One的杂货小窝 接入百度人脸的demo时候,发现了内置的switch开关比较好看,看了下实现方法,原来只是改了下样式,记录一下 效果: ...

  5. Redis无法向磁盘写入RBD数据

    2020-12-09 11:52:25|21965|ERROR|storage/DRedisAsyncCallback.cpp:394[cbIncrby]Cmd 'INCRBY' failed, ke ...

  6. 面试官:小伙子知道synchronized的优化过程吗?我:嘚吧嘚吧嘚,面试官:出去!

    写在开头 面试官:小伙子,多线程中锁用过吗? 我:那是自然! 面试官:那你知道synchronized的优化吗? 我:synchronized作为重锁,开销大,在早期不被推荐使用,后期进行了优化,至于 ...

  7. 容器镜像加速指南:探索 Kubernetes 缓存最佳实践

    介绍 将容器化应用程序部署到 Kubernetes 集群时,由于从 registry 中提取必要的容器镜像需要时间,因此可能会出现延迟.在应用程序需要横向扩展或处理高速实时数据的情况下,这种延迟尤其容 ...

  8. 关于FTP文件传输协议说明,带你了解更详情的文件传输协议

    Internet和其他网络上的人与设备之间的通信使用协议进行.您可以说协议定义了对话规则:谁必须在何时发送哪些信息?如果数据没有到达接收者,会发生什么?您如何保护转帐免受错误和犯规?每当我们使用Int ...

  9. quartus之rom的IP测试

    quartus之rom的IP测试 1.rom的作用 rom,就是只读存储器,内部数据在下载电路时就已经确认,不能使用信号驱动更改,只能够读取,一般用于比较重要的配置数据.在quartus中,可以直接调 ...

  10. KingbaseES V8R3集群运维案例---failover切换故障分析

    案例说明: KingbaseES V8R3集群主库数据库服务重启后,failover切换失败,分析failover失败的具体原因. 适用版本: KingbaseES V8R3 一.集群架构 node1 ...