当前位置:网站首页>LeetCode 542、01 矩阵
LeetCode 542、01 矩阵
2022-04-23 20:23:00 【亡于灬】
542、01 矩阵
1)题目描述
给定一个由 0
和 1
组成的矩阵 mat
,请输出一个大小相同的矩阵,其中每一个格子是 mat
中对应位置元素到最近的 0
的距离。
两个相邻元素间的距离为 1
。
示例 1:
输入:mat = [[0,0,0],[0,1,0],[0,0,0]]
输出:[[0,0,0],[0,1,0],[0,0,0]]
示例 2:
输入:mat = [[0,0,0],[0,1,0],[1,1,1]]
输出:[[0,0,0],[0,1,0],[1,2,1]]
提示:
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
中至少有一个0
2)分析
广度优先搜索。
存储所有 0 的位置,入队,然后遍历队列,如果四周存在 1 ,那么设置步数,然后添加进队列。
3)C++
代码
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};
//访问所有的零并将0加入队列
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;
}
}
}
//广度优先搜索
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;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124360368
边栏推荐
- The market share of the financial industry exceeds 50%, and zdns has built a solid foundation for the financial technology network
- R language ggplot2 visual facet_wrap, and use the lineheight parameter to customize the height of the facet icon tab (gray label bar)
- R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of
- Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
- How to do product innovation—— Exploration of product innovation methodology I
- 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
- Automatically fill in body temperature and win10 task plan
- Monte Carlo py solves the area problem! (save pupils Series)
- Why does ES6 need to introduce map when JS already has object type
猜你喜欢
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
How can matlab obtain the truncated image in trainingimagelabeler
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
LeetCode动态规划训练营(1~5天)
网络通信基础(局域网、广域网、IP地址、端口号、协议、封装、分用)
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
Automatically fill in body temperature and win10 task plan
Mysql database backup scheme
After route link navigation, the sub page does not display the navigation style problem
随机推荐
Local call feign interface message 404
Numpy Index & slice & iteration
Rédaction de thèses 19: différences entre les thèses de conférence et les thèses périodiques
WordPress插件:WP-China-Yes解决国内访问官网慢的方法
Markdown < a > tag new page open link
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
Sqoop imports tinyint type fields to boolean type
Handwritten Google's first generation distributed computing framework MapReduce
Notes of Tang Shu's grammar class in postgraduate entrance examination English
The ODB model calculates the data and outputs it to excel
R语言使用timeROC包计算存在竞争风险情况下的生存资料多时间AUC值、使用cox模型、并添加协变量、R语言使用timeROC包的plotAUCcurve函数可视化多时间生存资料的AUC曲线
Es error: request contains unrecognized parameter [ignore_throttled]
Remote code execution in Win 11 using wpad / PAC and JScript 1
An error is reported in the initialization metadata of the dolphin scheduler -- it turns out that there is a special symbol in the password. "$“
【目标跟踪】基于帧差法结合卡尔曼滤波实现行人姿态识别附matlab代码
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
Some basic configurations in interlij idea
What is the difference between a host and a server?
A useless confession artifact