当前位置:网站首页>leetcode-304:二维区域和检索 - 矩阵不可变
leetcode-304:二维区域和检索 - 矩阵不可变
2022-07-07 08:17:00 【菊头蝙蝠】
题目
给定一个二维矩阵 matrix,以下类型的多个请求:
- 计算其子矩形范围内元素的总和,该子矩阵的 左上角 为 (row1, col1) ,右下角 为 (row2, col2) 。
实现 NumMatrix 类:
- NumMatrix(int[][] matrix) 给定整数矩阵 matrix 进行初始化
- int sumRegion(int row1, int col1, int row2, int col2) 返回 左上角 (row1, col1) 、右下角 (row2, col2) 所描述的子矩阵的元素 总和 。
示例 1:
输入:
["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 (蓝色矩形框的元素总和)
解题
方法一:前缀和
preSums[i][j]
表示左上角为(0,0),右下角索引为(i,j)的矩阵里的所有元素和
1、首先通过下述递推关系式计算出preSums[i][j]
S ( O , D ) = S ( O , C ) + S ( O , B ) − S ( O , A ) + D S(O,D)=S(O,C)+S(O,B)−S(O,A)+D S(O,D)=S(O,C)+S(O,B)−S(O,A)+D
2、根据已经计算出的preSums,可以得出想要计算的矩阵中所有元素之和
S ( A , D ) = S ( O , D ) − S ( O , E ) − S ( O , F ) + S ( O , G ) S(A,D)=S(O,D)−S(O,E)−S(O,F)+S(O,G) S(A,D)=S(O,D)−S(O,E)−S(O,F)+S(O,G)
class NumMatrix {
public:
vector<vector<int>> preSums;
NumMatrix(vector<vector<int>>& matrix) {
int m=matrix.size();
int n=matrix[0].size();
preSums.resize(m+1,vector<int>(n+1));
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
preSums[i+1][j+1]=preSums[i+1][j]+preSums[i][j+1]-preSums[i][j]+matrix[i][j];
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
return preSums[row2+1][col2+1]-preSums[row1][col2+1]-preSums[row2+1][col1]+preSums[row1][col1];
}
};
边栏推荐
猜你喜欢
P1031 [NOIP2002 提高组] 均分纸牌
【acwing】789. Range of numbers (binary basis)
Serial communication relay Modbus communication host computer debugging software tool project development case
0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)
IO模型复习
STM32 ADC和DMA
STM32 ADC and DMA
php \n 换行无法输出
Appx code signing Guide
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
随机推荐
@Transcation的配置,使用,原理注意事项:
ORM -- grouping query, aggregation query, query set queryset object properties
Sword finger offer 38 Arrangement of strings [no description written]
【二开】【JeecgBoot】修改分页参数
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
基于gis三维可视化技术的智慧城市建设
Some properties of leetcode139 Yang Hui triangle
Postman interface test I
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
【acwing】786. Number k
STM32产品介绍
JMeter loop controller and CSV data file settings are used together
A small problem of bit field and symbol expansion
Methods of adding centerlines and centerlines in SolidWorks drawings
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
1323:【例6.5】活动选择
EasyExcel读取写入简单使用
【acwing】789. Range of numbers (binary basis)
Study summary of postgraduate entrance examination in November