当前位置:网站首页>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];
}
};
边栏推荐
- Pdf document signature Guide
- Study summary of postgraduate entrance examination in July
- XML configuration file parsing and modeling
- 施努卡:机器视觉定位技术 机器视觉定位原理
- Slurm资源管理与作业调度系统安装配置
- STM32 Basics - memory mapping
- Why is the reflection efficiency low?
- Prototype object in ES6
- Differences between MCU and MPU
- 【作业】2022.7.6 写一个自己的cal函数
猜你喜欢
Appx代码签名指南
基于gis三维可视化技术的智慧城市建设
Some properties of leetcode139 Yang Hui triangle
Use the fetch statement to obtain the repetition of the last row of cursor data
Postman interface test VI
Smart city construction based on GIS 3D visualization technology
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
Fiddler simulates the interface test
Programming features of ISP, IAP, ICP, JTAG and SWD
leetcode-560:和为 K 的子数组
随机推荐
Factorial implementation of large integer classes
Experience sharing of software designers preparing for exams
IO model review
XML configuration file parsing and modeling
Easyexcel read write simple to use
table宽度比tbody宽度大4px
深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
MySQL insert data create trigger fill UUID field value
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
Use the fetch statement to obtain the repetition of the last row of cursor data
ORM -- database addition, deletion, modification and query operation logic
STM32 product introduction
Learning records - high precision addition and multiplication
P1031 [NOIP2002 提高组] 均分纸牌
Multisim--软件相关使用技巧
Talking about the return format in the log, encapsulation format handling, exception handling
【剑指Offer】42. 栈的压入、弹出序列
Guid主键
【acwing】789. 数的范围(二分基础)
LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件