当前位置:网站首页>Leetcode-304: two dimensional area and retrieval - matrix immutable
Leetcode-304: two dimensional area and retrieval - matrix immutable
2022-07-07 10:27:00 【Chrysanthemum headed bat】
leetcode-304: Two dimensional region and retrieval - The matrix is immutable
subject
Given a two-dimensional matrix matrix, Multiple requests of the following types :
- Calculate the sum of the elements in its subrectangle , Of the submatrix top left corner by (row1, col1) , The lower right corner by (row2, col2) .
Realization NumMatrix class :
- NumMatrix(int[][] matrix) Given an integer matrix matrix To initialize
- int sumRegion(int row1, int col1, int row2, int col2) return top left corner (row1, col1) 、 The lower right corner (row2, col2) The elements of the described submatrix The sum of the .
Example 1:
Input :
["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]]
Output :
[null, 8, 11, 12]
explain :
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 ( The sum of the elements of the red rectangle )
numMatrix.sumRegion(1, 1, 2, 2); // return 11 ( The sum of the elements of the green rectangle )
numMatrix.sumRegion(1, 2, 2, 4); // return 12 ( Sum of elements in blue rectangle )
Problem solving
Method 1 : The prefix and
preSums[i][j]
The upper left corner is (0,0), The index in the lower right corner is (i,j) All elements in the matrix of and
1、 First, calculate 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、 According to the calculated preSums, You can get the sum of all the elements in the matrix you want to calculate
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];
}
};
边栏推荐
- The request object parses the request body and request header parameters
- leetcode-303:区域和检索 - 数组不可变
- P1031 [NOIP2002 提高组] 均分纸牌
- ArcGIS operation: batch modify attribute table
- JMeter about setting thread group and time
- Adb 实用命令(网络包、日志、调优相关)
- @Configuration, use, principle and precautions of transmission:
- 【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
- The width of table is 4PX larger than that of tbody
- Study summary of postgraduate entrance examination in September
猜你喜欢
使用U2-Net深层网络实现——证件照生成程序
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
Remote meter reading, switching on and off operation command
0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
Several schemes of building hardware communication technology of Internet of things
【acwing】789. 数的范围(二分基础)
XML configuration file parsing and modeling
Postman interface test VI
Programming features of ISP, IAP, ICP, JTAG and SWD
随机推荐
Yarn的基础介绍以及job的提交流程
The request object parses the request body and request header parameters
Pdf document signature Guide
反射效率为什么低?
Talking about the return format in the log, encapsulation format handling, exception handling
[second on] [jeecgboot] modify paging parameters
Programming features of ISP, IAP, ICP, JTAG and SWD
Can I open a stock trading account online? Is it safe
字符串格式化
How to cancel automatic saving of changes in sqlyog database
Appx代码签名指南
JMeter installation
@Configuration, use, principle and precautions of transmission:
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
宁愿把简单的问题说一百遍,也不把复杂的问题做一遍
Jump to the mobile terminal page or PC terminal page according to the device information
Postman interface test III
Fiddler break point
The method of word automatically generating directory
IDA中常见快捷键