当前位置:网站首页>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];
}
};
边栏推荐
猜你喜欢
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
Several schemes of building hardware communication technology of Internet of things
String formatting
【二开】【JeecgBoot】修改分页参数
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
基于gis三维可视化技术的智慧城市建设
随机推荐
施努卡:机器人视觉抓取工作原理 机器视觉抓取
深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
Download Text, pictures and ab packages used by unitywebrequest Foundation
Mongodb creates an implicit database as an exercise
求最大公约数与最小公倍数(C语言)
嵌入式工程师如何提高工作效率
BigDecimal value comparison
搭建物联网硬件通信技术几种方案
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
How embedded engineers improve work efficiency
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
Can I open a stock trading account online? Is it safe
【acwing】786. 第k个数
@Configuration, use, principle and precautions of transmission:
Fiddler simulates the interface test
Study summary of postgraduate entrance examination in August
openinstall与虎扑达成合作,挖掘体育文化产业数据价值
2022.7.3DAY595
Slurm资源管理与作业调度系统安装配置
Remote meter reading, switching on and off operation command