当前位置:网站首页>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
边栏推荐
- Solution to PowerDesigner's failure to connect to MySQL in x64 system
- An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
- Latest investigation and progress of building intelligence based on sati
- Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
- DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
- Livego + ffmpeg + RTMP + flvjs to realize live video
- Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
- R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of
- 黑客的入侵方式你知道几种?
- LeetCode 709、转换成小写字母
猜你喜欢
Livego + ffmpeg + RTMP + flvjs to realize live video
Handwritten Google's first generation distributed computing framework MapReduce
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
Operation of numpy array
Installation and use of NVM
An error is reported when sqoop imports data from Mysql to HDFS: sqlexception in nextkeyvalue
[latex] 5 how to quickly write out the latex formula corresponding to the formula
. Ren -- the intimate artifact in the field of vertical Recruitment!
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
What is the difference between a host and a server?
随机推荐
LeetCode 542、01 矩阵
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind
16MySQL之DCL 中 COMMIT和ROllBACK
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 64
【PTA】L1-006 连续因子
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
【栈和队列专题】—— 滑动窗口
【问题解决】‘ascii‘ codec can‘t encode characters in position xx-xx: ordinal not in range(128)
Markdown < a > tag new page open link
[problem solving] 'ASCII' codec can't encode characters in position XX XX: ordinal not in range (128)
LeetCode 20、有效的括号
Use the rolling division method to find the maximum common divisor of two numbers
After route link navigation, the sub page does not display the navigation style problem
Some basic configurations in interlij idea
[PTA] l2-011 play with binary tree
Rt-1052 learning notes - GPIO architecture analysis
考研英语唐叔的语法课笔记