当前位置:网站首页>Sword finger offer II 107 Distance in matrix
Sword finger offer II 107 Distance in matrix
2022-07-07 09:48:00 【PI Qiliang】
The finger of the sword Offer II 107. The distance in the matrix 【 Medium question 】
Ideas :
use BFS Sequence traversal , Find all the elements 0, From element 0 Start sequence traversal in four directions , set up ans The array stores the latest 0 distance , be ans in ,mat Matrix elements 0 The position value is 0, Encountered during traversal of each layer 1 Will ans Value is updated to element 0 Of ans value +1
Code :
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;
// Mark mat matrix (i,j) Whether the location element has been searched
boolean[][] seen = new boolean[m][n];
// Storage mat In the matrix 0 The position coordinates of
Queue<int[]> queue = new LinkedList<>();
// Storage mat matrix (i,j) The nearest location element 0 Distance of
ans = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 0){
// take mat The elements in the matrix 0 The position coordinates of are pressed into the queue
queue.offer(new int[]{
i,j});
// The current location of the tag has been searched
seen[i][j] = true;
}
}
}
//BFS Traverse , When the element in the stack is empty , The loop ends
while(!queue.isEmpty()){
// Take out the top element of the queue 0 The location of
int[] cell = queue.poll();
// Take out the elements 0 Coordinates of
int i = cell[0],j = cell[1];
for (int k = 0; k < 4; k++) {
// The element 0 Search up, down, left and right , The location coordinates searched are (x,y)
int x = i + dir[k][0],y = j + dir[k][1];
// If (x,y) Does not exceed the matrix boundary and (x,y) The location has not been searched
if (x >= 0 && x < m && y >= 0 && y < n && !seen[x][y]){
//ans Of (x,y) Position value be equal to ans Of (i,j) Position value + 1
ans[x][y] = ans[i][j] + 1;
// take (x,y) Position push into queue
queue.offer(new int[]{
x,y});
// take (x,y) The location is marked as searched
seen[x][y] = true;
}
}
}
return ans;
}
}
边栏推荐
- VSCode+mingw64+cmake
- How to use clipboard JS library implements copy and cut function
- js逆向教程第二发-猿人学第一题
- 如何成为一名高级数字 IC 设计工程师(5-3)理论篇:ULP 低功耗设计技术精讲(下)
- 洛谷P2482 [SDOI2010]猪国杀
- Information Security Experiment 3: the use of PGP email encryption software
- shake数据库中怎么使用Mongo-shake实现MongoDB的双向同步啊?
- Arthas simple instructions
- 章鱼未来之星获得25万美金奖励|章鱼加速器2022夏季创业营圆满落幕
- Vs2013 generate solutions super slow solutions
猜你喜欢

# Arthas 简单使用说明
![[4G/5G/6G专题基础-147]: 6G总体愿景与潜在关键技术白皮书解读-2-6G发展的宏观驱动力](/img/21/6a183e4e10daed90c66235bdbdc3bf.png)
[4G/5G/6G专题基础-147]: 6G总体愿景与潜在关键技术白皮书解读-2-6G发展的宏观驱动力

Netease cloud wechat applet

小程序弹出半角遮罩层

VSCode+mingw64+cmake

Information Security Experiment 2: using x-scanner scanning tool

Over 100000 words_ Ultra detailed SSM integration practice_ Manually implement permission management

印象笔记终于支持默认markdown预览模式

如何使用clipboard.js库实现复制剪切功能

4、 Fundamentals of machine learning
随机推荐
VSCode+mingw64+cmake
Scratch crawler mysql, Django, etc
Information Security Experiment 2: using x-scanner scanning tool
MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
Netease cloud wechat applet
数据建模中利用3σ剔除异常值进行数据清洗
flinkcdc采集oracle在snapshot阶段一直失败,这个得怎么调整啊?
根据热门面试题分析Android事件分发机制(二)---事件冲突分析处理
剑指 Offer II 107. 矩阵中的距离
如何成为一名高级数字 IC 设计工程师(5-2)理论篇:ULP 低功耗设计技术精讲(上)
基于智慧城市与储住分离数字家居模式垃圾处理方法
NETCORE 3.1 solves cross domain problems
Pit using BigDecimal
在EXCEL写VBA连接ORACLE并查询数据库中的内容
12、 Sort
有没有大佬帮忙看看这个报错,有啥排查思路,oracle cdc 2.2.1 flink 1.14.4
Vs2013 generate solutions super slow solutions
【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
Database multi table Association query problem
14th test