当前位置:网站首页>Leetcode 542, 01 matrix
Leetcode 542, 01 matrix
2022-04-23 20:26:00 【Die in a trap】
542、01 matrix
1) Title Description
Given a by 0
and 1
Matrix of composition mat
, Please output a matrix of the same size , Each of these grids is mat
From the corresponding position element to the nearest 0
Distance of .
The distance between two adjacent elements is 1
.
Example 1:
Input :mat = [[0,0,0],[0,1,0],[0,0,0]]
Output :[[0,0,0],[0,1,0],[0,0,0]]
Example 2:
Input :mat = [[0,0,0],[0,1,0],[1,1,1]]
Output :[[0,0,0],[0,1,0],[1,2,1]]
Tips :
m == mat.length
n == mat[i].length
1 <= m, n <= 10^4
1 <= m * n <= 10^4
mat[i][j] is either 0 or 1.
mat
At least one of them0
2) analysis
Breadth first search .
Store all 0 The location of , The team , Then traverse the queue , If there are around 1 , Then set the number of steps , Then add it to the queue .
3)C++
Code
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int m=mat.size();
int n=mat[0].size();
vector<vector<int>> seen(m,vector<int> (n,0));
vector<vector<int>> distance(m,vector<int> (n,0));
queue<pair<int,int>> que;
int dx[4]={
1,0,-1,0};
int dy[4]={
0,1,0,-1};
// Access all zeros and put 0 Join the queue
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(!mat[i][j]){
que.emplace(i,j);
seen[i][j]=1;
}
}
}
// Breadth first search
while(!que.empty()){
int x=que.front().first;
int y=que.front().second;
que.pop();
for(int i=0;i<4;i++){
int newX=x+dx[i];
int newY=y+dy[i];
if(newX>=0&&newX<m&&newY>=0&&newY<n&&!seen[newX][newY]){
seen[newX][newY]=1;
distance[newX][newY]=distance[x][y]+1;
que.emplace(newX,newY);
}
}
}
return distance;
}
};
版权声明
本文为[Die in a trap]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232023107558.html
边栏推荐
- Three. Based on ply format point cloud voxel model JS upload interface writing
- 考研英语唐叔的语法课笔记
- Customize timeline component styles
- I JS deep copy and shallow copy
- Rt-1052 learning notes - GPIO architecture analysis
- LeetCode 709、转换成小写字母
- Alicloud: could not connect to SMTP host: SMTP 163.com, port: 25
- SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
- Use the rolling division method to find the maximum common divisor of two numbers
- Recognition of high-speed road signs by Matlab using alexnet
猜你喜欢
Linux64Bit下安装MySQL5.6-不能修改root密码
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
How to protect ECs from hacker attacks?
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
Monte Carlo py solves the area problem! (save pupils Series)
C migration project record: modify namespace and folder name
The ODB model calculates the data and outputs it to excel
Automatically fill in body temperature and win10 task plan
Vscode download speed up
Notes of Tang Shu's grammar class in postgraduate entrance examination English
随机推荐
上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
Experience of mathematical modeling in 18 year research competition
Solve the Chinese garbled code of URL in JS - decoding
Vscode download speed up
Cadence Orcad Capture CIS更换元器件之Link Database 功能介绍图文教程及视频演示
Servlet learning notes
LeetCode 1346、检查整数及其两倍数是否存在
Three. Based on ply format point cloud voxel model JS upload interface writing
三十.什么是vm和vc?
Solution to PowerDesigner's failure to connect to MySQL in x64 system
[latex] 5 how to quickly write out the latex formula corresponding to the formula
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
After route link navigation, the sub page does not display the navigation style problem
[PTA] l1-006 continuity factor
BMP JPEG 图片转换为矢量图像 ContourTrace
Computing the intersection of two planes in PCL point cloud processing (51)
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
Numpy Index & slice & iteration
star
【栈和队列专题】—— 滑动窗口