当前位置:网站首页>剑指 Offer II 107. 矩阵中的距离
剑指 Offer II 107. 矩阵中的距离
2022-07-07 07:07:00 【彼淇梁】
剑指 Offer II 107. 矩阵中的距离【中等题】
思路:
采用BFS层序遍历,找出所有的元素0,从元素0开始向四个方向层序遍历,设ans数组存储最近0距离,则ans中,mat矩阵元素0位置值为0,每层遍历遇到1则将ans值更新为元素0的ans值+1
代码:
class Solution {
static int[][] ans;
static int[][] dir = {
{
0,-1},{
0,1},{
-1,0},{
1,0}};
static int m,n;
public static int[][] updateMatrix(int[][] mat) {
m = mat.length;
n = mat[0].length;
//标记mat矩阵(i,j)位置元素是否被搜索过
boolean[][] seen = new boolean[m][n];
//存储mat矩阵中的0的位置坐标
Queue<int[]> queue = new LinkedList<>();
//存储mat矩阵(i,j)位置元素最近的0的距离
ans = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 0){
//将mat矩阵中元素0的位置坐标压入队列
queue.offer(new int[]{
i,j});
//标记当前位置已被搜索过
seen[i][j] = true;
}
}
}
//BFS遍历,当栈中元素为空时,循环结束
while(!queue.isEmpty()){
//取出队列顶部元素0的位置
int[] cell = queue.poll();
//取出元素0的坐标
int i = cell[0],j = cell[1];
for (int k = 0; k < 4; k++) {
//对元素0进行上下左右四个方向搜索,搜索到的位置坐标为(x,y)
int x = i + dir[k][0],y = j + dir[k][1];
//如果(x,y)未超出矩阵边界且(x,y)位置未被搜索过
if (x >= 0 && x < m && y >= 0 && y < n && !seen[x][y]){
//ans的(x,y)位置值 等于 ans的(i,j)位置值 + 1
ans[x][y] = ans[i][j] + 1;
//将(x,y)位置压入队列
queue.offer(new int[]{
x,y});
//将(x,y)位置标记为已搜索
seen[x][y] = true;
}
}
}
return ans;
}
}
边栏推荐
- Lecture 1: stack containing min function
- 印象笔记终于支持默认markdown预览模式
- 12、 Sort
- 数据建模中利用3σ剔除异常值进行数据清洗
- 十二、排序
- 细说Mysql MVCC多版本控制
- 牛客网——华为题库(61~70)
- In fact, it's very simple. It teaches you to easily realize the cool data visualization big screen
- ViewPager2和VIewPager的区别以及ViewPager2实现轮播图
- PostgreSQL reports an error when creating a trigger,
猜你喜欢
Arthas simple instructions
[4G/5G/6G专题基础-147]: 6G总体愿景与潜在关键技术白皮书解读-2-6G发展的宏观驱动力
[cloud native] Devops (I): introduction to Devops and use of code tool
What is MD5
Detailed explanation of diffusion model
Connecting mobile phone with ADB
Information Security Experiment 4: implementation of IP packet monitoring program
Jenkins+ant+jmeter use
[bw16 application] Anxin can realize mqtt communication with bw16 module / development board at instruction
Information Security Experiment 3: the use of PGP email encryption software
随机推荐
Elaborate on MySQL mvcc multi version control
面试被问到了解哪些开发模型?看这一篇就够了
Huawei HCIP - datacom - Core 03 jours
Difference between interface iterator and iteratable
Netease Cloud Wechat applet
The configuration and options of save actions are explained in detail, and you won't be confused after reading it
AI从感知走向智能认知
在EXCEL写VBA连接ORACLE并查询数据库中的内容
thinkphp3.2信息泄露
NATAPP内网穿透
How to solve the problem of golang select mechanism and timeout
如何成为一名高级数字 IC 设计工程师(1-6)Verilog 编码语法篇:经典数字 IC 设计
JMeter JDBC batch references data as input parameters (the simplest method for the whole website)
Information Security Experiment 3: the use of PGP email encryption software
【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
沙龙预告|GameFi 领域的瓶颈和解决方案
(3/8) method parameters of improper use of enumeration (2)
Unity shader (basic concept)
Pick up the premise idea of programming
Yapi test plug-in -- cross request