当前位置:网站首页>leetcode:304. 2D area and retrieval - matrix immutable
leetcode:304. 2D area and retrieval - matrix immutable
2022-06-29 03:35:00 【Oceanstar's learning notes】
Title source
Title Description


class NumMatrix {
public:
NumMatrix(vector<vector<int>>& matrix) {
}
int sumRegion(int row1, int col1, int row2, int col2) {
}
};
title
This problem requires finding the sum of submatrixes
violence
class NumMatrix {
vector<vector<int>> mt;
public:
NumMatrix(vector<vector<int>>& matrix) {
this->mt = matrix;
}
int sumRegion(int row1, int col1, int row2, int col2) {
int sum = 0;
for (int i = row1; i <= row2; ++i) {
for (int j = col1; j <= col2; ++j) {
sum += mt[i][j];
}
}
return sum;
}
};
One dimensional prefixes and
We can think of two-dimensional arrays as multiple one-dimensional arrays , And find the prefix sum of all one-dimensional data , Then calculate the sum of the matrix

class NumMatrix {
vector<vector<int>> preSum;
public:
NumMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(); // Take the number of lines
if(m > 0){
int n = matrix[0].size(); // Take the number of columns
preSum.resize(m, std::vector<int>(n + 1)); // Create a new one One more than before 0 A binary array of columns
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
preSum[i][j + 1] = preSum[i][j] + matrix[i][j];
}
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
int sum = 0;
// Add up each line
for (int i = row1; i <= row2; ++i) {
sum += preSum[i][col2 + 1] - preSum[i][col1];
}
return sum;
}
};
Two dimensional prefixes and
The so-called two-dimensional prefix and , That is to say (i,j) As the lower right corner ,(0,0) As the sum of a rectangle in the upper left corner 

So how do we calculate preSum(i,j) The value of ?

therefore :preSum(i,j) = preSum(i - 1,j) + preSum(i,j - 1) - preSum(i - 1,j - 1) + matrix(i,j)
With the prefix and we can calculate the value of the middle rectangle 
therefore :result = preSum(row2 + 1,col2 + 1) - preSum(row1,col2 + 1) - preSum(row2 + 1,col1) + preSum(row1,col1);
class NumMatrix {
vector<vector<int>> preSum;
public:
NumMatrix(vector<vector<int>>& matrix) {
if(!matrix.empty() || !matrix[0].empty()){
return;
}
int m = matrix.size(), n = matrix[0].size();
preSum.resize(m + 1, std::vector<int>(n + 1, 0));
for (int i = 1; i <= m; ++i) {
for (int j = 1; j < n; ++j) {
// Calculate each matrix [0, 0, i, j] Elements and
preSum[i][j] =
preSum[i-1][j] + preSum[i][j-1] +
matrix[i - 1][j - 1] - preSum[i-1][j-1];
}
}
}
// Calculate the submatrix [x1, y1, x2, y2] Elements and
int sumRegion(int row1, int col1, int row2, int col2) {
// The sum of the target matrix is obtained by the operation of four adjacent matrices
return preSum[row2 + 1][col2 + 1] - preSum[row2 + 1][col1] - preSum[row1][col2 + 1] + preSum[row1][col1];
}
};
边栏推荐
- [dynamic planning] change exchange
- Etcd教程 — 第七章 Etcd之事务API
- [World Ocean Day] tcapulusdb calls on you to protect marine biodiversity together
- Mobaihe box, ZTE box, Migu box, Huawei box, Huawei Yuehe box, Fiberhome box, Skyworth box, Tianyi box and other operators' box firmware collection and sharing
- 分布式id解决方案
- Synchronous real-time data of Jerry's watch [chapter]
- gcc编译器包
- Problème - Ajouter shellerror: permissions d'instrumentation pour le périphérique: vérifier les règles udev.
- Connect error: no route to host (errno:113)
- 初探元宇宙存储,数据存储市场下一个爆点?
猜你喜欢

Linear and nonlinear structures

Geth --- Error: authentication needed: password or unlock

19.03 vessel description and simple application examples continued

Grafana Getting Started tutorial

go实现分布式锁

87. (cesium chapter) cesium thermal map (pasted with terrain)

leetcode:560. 和为 K 的子数组

87.(cesium篇)cesium热力图(贴地形)

设备监理师证书含金量怎样?值得考吗?

How to keep source code secret in embedded development
随机推荐
Zigzag sequence traversal of binary tree [one of layered traversal methods - > preorder traversal +level]
设备监理师证书含金量怎样?值得考吗?
leetcode:560. 和为 K 的子数组
[tcapulusdb knowledge base] Introduction to tcapulusdb table data caching
[thread communication]
Unable to locate program input point [email protected]
【线程通信】
What is the gold content of the equipment supervisor certificate? Is it worth it?
Probe into metacosmic storage, the next explosive point in the data storage market?
Gartner“客户之声”最高分,用户体验成中国数据库一大突破口
凌晨三点学习的你,感到迷茫了吗?
MATALB signal processing - signal transformation (6)
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(三)
Geth --- Error: authentication needed: password or unlock
How to understand MySQL indexes?
高性能限流器 Guava RateLimiter
Digital twin application of smart Park Based on Web GIS aerial photography
[tcapulusdb knowledge base] modify business modify cluster
【TcaplusDB知识库】TcaplusDB技术支持介绍
[test theory] quality analysis ability