当前位置:网站首页>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];
}
};
边栏推荐
- Study summary of postgraduate entrance examination in July
- UnityWebRequest基础使用之下载文本、图片、AB包
- 基于gis三维可视化技术的智慧城市建设
- Guide de signature du Code Appx
- JMeter about setting thread group and time
- PDF文档签名指南
- This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
- CONDA creates virtual environment offline
- STM32 ADC和DMA
- 深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
猜你喜欢
求方程ax^2+bx+c=0的根(C语言)
OpenGL glLightfv 函数的应用以及光源的相关知识
The request object parses the request body and request header parameters
Yarn的基础介绍以及job的提交流程
5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
Some properties of leetcode139 Yang Hui triangle
[ORM framework]
浅谈日志中的返回格式封装格式处理,异常处理
对word2vec的一些浅层理解
XML configuration file parsing and modeling
随机推荐
The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.
SQLyog数据库怎么取消自动保存更改
大整数类实现阶乘
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
Adb 实用命令(网络包、日志、调优相关)
555电路详解
STM32 ADC和DMA
Embedded background - chip
JMeter loop controller and CSV data file settings are used together
Smart city construction based on GIS 3D visualization technology
【acwing】786. Number k
Appx code signing Guide
Guide de signature du Code Appx
Talking about the return format in the log, encapsulation format handling, exception handling
Advanced function learning in ES6
Postman interface test II
Guid主键
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?