当前位置:网站首页>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];
}
};
边栏推荐
- ORM model -- creation and query of data records
- SolidWorks工程图中添加中心线和中心符号线的办法
- 大整数类实现阶乘
- 学习记录——高精度加法和乘法
- [second on] [jeecgboot] modify paging parameters
- ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
- JMeter loop controller and CSV data file settings are used together
- The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
- IPv4套接字地址结构
- A small problem of bit field and symbol expansion
猜你喜欢

IO模型复习

JMeter about setting thread group and time

ArcGIS operation: batch modify attribute table

The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file

搭建物联网硬件通信技术几种方案

对word2vec的一些浅层理解

ORM -- logical relation and & or; Sort operation, update record operation, delete record operation

STM32 Basics - memory mapping

【acwing】786. 第k个数

ISP、IAP、ICP、JTAG、SWD的编程特点
随机推荐
反射效率为什么低?
ES6中的函數進階學習
Postman interface test IV
字符串格式化
SQLyog数据库怎么取消自动保存更改
STM32产品介绍
Use the fetch statement to obtain the repetition of the last row of cursor data
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
【acwing】786. 第k个数
Pdf document signature Guide
P1031 [NOIP2002 提高组] 均分纸牌
The request object parses the request body and request header parameters
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
JMeter installation
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
table宽度比tbody宽度大4px
Download Text, pictures and ab packages used by unitywebrequest Foundation
. Net configuration system
Es classes and objects, prototypes