当前位置:网站首页>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.lengthn == mat[i].length1 <= m, n <= 10^41 <= m * n <= 10^4mat[i][j] is either 0 or 1.matAt 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
边栏推荐
- The flinkcdc reports an error: but this is no longer available on the server
- Recommend an open source free drawing software draw IO exportable vector graph
- Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
- SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
- JDBC database addition, deletion, query and modification tool class
- SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
- Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
- How do BIM swindlers cheat? (turn)
- Cadence OrCAD capture batch change component packaging function introduction graphic tutorial and video demonstration
- Some basic configurations in interlij idea
猜你喜欢

Recommend an open source free drawing software draw IO exportable vector graph

Sqoop imports tinyint type fields to boolean type

Livego + ffmpeg + RTMP + flvjs to realize live video

Leetcode dynamic planning training camp (1-5 days)

Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order

16MySQL之DCL 中 COMMIT和ROllBACK

Numpy mathematical function & logical function

Numpy - creation of data type and array

LeetCode 542、01 矩阵

Commit and ROLLBACK in DCL of 16mysql
随机推荐
Recognition of high-speed road signs by Matlab using alexnet
Why does ES6 need to introduce map when JS already has object type
Numpy Index & slice & iteration
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
SQL: query duplicate data and delete duplicate data
Intersection calculation of straight line and plane in PCL point cloud processing (53)
Introduction to link database function of cadence OrCAD capture CIS replacement components, graphic tutorial and video demonstration
【PTA】L1-006 连续因子
Click an EL checkbox to select all questions
star
PCA based geometric feature calculation of PCL point cloud processing (52)
How does onlyoffice solve no route to host
Vscode download speed up
ArcGIS js api 4. X submergence analysis and water submergence analysis
R language ggplot2 visualization: ggplot2 visualizes the scatter diagram and uses geom_ mark_ The ellipse function adds ellipses around data points of data clusters or data groups for annotation
Still using listview? Use animatedlist to make list elements move
. Ren -- the intimate artifact in the field of vertical Recruitment!
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
論文寫作 19: 會議論文與期刊論文的區別
Some basic knowledge of devexpress report development