当前位置:网站首页>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];
}
};
边栏推荐
- 二叉树的层序遍历 II[层序遍历方式之一 ->递归遍历 + level]
- 二叉树的锯齿形层序遍历[分层遍历方式之一 -> 前序遍历+level]
- 【TcaplusDB知识库】TcaplusDB-tcaplusadmin工具介绍
- 4种分布式session解决方案
- How to keep source code secret in embedded development
- An internal error occurred during: 'Retrieving archetypes:'.
- 图扑软件智慧能源一体化管控平台
- Faster memcpy alternatives- faster alternative to memcpy?
- 【雲原生】這麼火,你不來了解下?
- 20款IDEA 神级插件 效率提升 30 倍,写代码必备
猜你喜欢

go-redsync分布式锁源码解析

Web APIs 高阶函数 丨黑马程序员

Digital twin application of smart Park Based on Web GIS aerial photography

How to keep source code secret in embedded development

【TcaplusDB知识库】TcaplusDB-tcapulogmgr工具介绍(二)

一个注解优雅的实现 接口数据脱敏

Go implements distributed locks

FPGA(七)RTL代码之三(复杂电路设计2)

Tupu software intelligent energy integrated management and control platform

leetcode:560. Subarray with and K
随机推荐
不同的二叉搜索樹[自下而上回溯生成樹+記憶搜索--空間換時間]
分布式id解决方案
Shell script to count files, then remove oldest files
Faster memcpy alternatives- faster alternative to memcpy?
Ugui slider minimum control
MySQL advanced SQL statement (Part 2)
[flutter topic] 66 diagram basic constraints box (I) yyds dry goods inventory
How to keep source code secret in embedded development
[tcapulusdb] I wish you all a healthy Dragon Boat Festival!
20款IDEA 神级插件 效率提升 30 倍,写代码必备
设备监理师证书含金量怎样?值得考吗?
FPGA (VII) RTL code III (complex circuit design 2)
【面试指南】AI算法面试
問題——adb shellerror: insufficient permissions for device: verify udev rules.
Use gstarwmr video conversion for yocto system of i.mx8m development board
Grafana入门教程
Laravel, execute PHP artist migrate and report an error alter table `users`add unique `users_ email_ unique`(`email`))
MATALB signal processing - signal transformation (6)
87.(cesium篇)cesium热力图(贴地形)
深度解析“链动2+1”模式的商业逻辑