【leetcode】二维子矩阵的和

一、 题目描述给定一个二维矩阵 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 (蓝色矩形框的元素总和)

二、 代码思路
2.1 暴力解法

直接嵌套for循环算即可

时间复杂度: n2

空间复杂度; mn数组大小

最大数字和为4*10^8 int 是可以搞定的。

2.2 前缀和

初始化时对矩阵的每一行计算前缀和,检索时对二维区域中的每一行计算子数组和,然后对每一行的子数组和计算总和。

具体实现方面,创建 m 行 n+1 列的二维数组 sums,其中 m 和 n 分别是矩阵 matrix 的行数和列数,sums[i] 为 matrix[i] 的前缀和数组。将 sums 的列数设为 n+1 的目的是为了方便计算每一行的子数组和,col1 = 0 的情况特殊处理。

作者:LeetCode-Solution
链接:https://leetcode.cn/problems/O4NDxx/solution/er-wei-zi-ju-zhen-de-he-by-leetcode-solu-vtih/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

三、 代码题解
class NumMatrix {

    private int[][] matrix;
    public NumMatrix(int[][] matrix) {
        this.matrix = matrix;
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        int res = 0;
        for(int i = row1; i <= row2; i++) {
            for(int j = col1; j <= col2; j++) {
                res += matrix[i][j];
            }
        }
        return res;         
    }
}

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */

时间复杂: 调用次数 * O(MN)

一维数组的前缀和:

class NumMatrix {

    private int[][] sum;
    public NumMatrix(int[][] matrix) {
        //本质上二维数组,只是数组的数组,也就是一维数组中每个元素再次存放一个数组
        //因此,一维数组的长度就是行数
        //一维数组中每个元素的长度就是列数
        int m = matrix.length;
        int n = matrix[0].length;
        int[][] sum = new int [m][n + 1];
        for(int i = 0; i < m; i++) {
            sum[i][0] = 0;
            for(int j = 1; j <= n; j++){
                sum[i][j] = sum[i][j - 1] + matrix[i][j - 1];
            }
        }
        this.sum = sum;
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        int res = 0;
        for(int i = row1; i <= row2; i++) {
            res += sum[i][col2 + 1] - sum[i][col1];  
        }
        return res;         
    }
}

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */

时间复杂度: 调用次数 * O(M) + O(MN)

空间复杂度: 0(MN)

四、 总结一下

前缀和思想的优点 :

1、 避免了大量的重复计算,提前将前缀元素的和计算出来,如果需要某一个子数组的元素和,则只需要sum[i] - sum[j] 即可,不需要再次重复计算一遍。

【leetcode】二维子矩阵的和

2、 比较方便求解子数组的和的问题,前缀和相减即可求得子数组之和。

【leetcode】左右两边子数组之和相等

3、 前缀和 + Map的思想,很方便求解子数组和为 0 的情况。

【leetcode】0和1个数相同的子数组

四、 参考资料

类型名称 取值范围
int -2^31~(2 ^31-1)
unsigned int 0~(2^32-1)
2^31=2,147,483,648 10^9数量级
2^32=4,294,967,296 10^9数量级

参考资料: java常见数据类型的范围大小以及java大数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值