当前位置:网站首页>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;
}
}
边栏推荐
- Selenium+bs4 parsing +mysql capturing BiliBili Tarot data
- C# Socke 服务器,客户端,UDP
- PostgreSQL reports an error when creating a trigger,
- [4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision
- 20排位赛3
- Lesson 1: hardness of eggs
- 创建一个长度为6的int型数组,要求数组元素的值都在1-30之间,且是随机赋值。同时,要求元素的值各不相同。
- 如何成为一名高级数字 IC 设计工程师(5-2)理论篇:ULP 低功耗设计技术精讲(上)
- Oracle installation enhancements error
- Huffman encoded compressed file
猜你喜欢
农牧业未来发展蓝图--垂直农业+人造肉
Lecture 1: stack containing min function
Software modeling and analysis
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
js逆向教程第二发-猿人学第一题
ComputeShader
Strategic cooperation subquery becomes the secret weapon of Octopus web browser
Information Security Experiment 2: using x-scanner scanning tool
Netease cloud wechat applet
VSCode+mingw64+cmake
随机推荐
How to speed up video playback in browser
其实特简单,教你轻松实现酷炫的数据可视化大屏
CDZSC_2022寒假个人训练赛21级(2)
战略合作|SubQuery 成为章鱼网络浏览器的秘密武器
Oracle安装增强功能出错
[4g/5g/6g topic foundation -147]: Interpretation of the white paper on 6G's overall vision and potential key technologies -2-6g's macro driving force for development
How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
NETCORE 3.1 solves cross domain problems
大佬们,请问 MySQL-CDC 有什么办法将 upsert 消息转换为 append only 消
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
VSCode+mingw64+cmake
进程间的通信方式
Difference between process and thread
How does mongodb realize the creation and deletion of databases, the creation of deletion tables, and the addition, deletion, modification and query of data
C# 初始化程序时查看初始化到哪里了示例
What development models did you know during the interview? Just read this one
【原创】程序员团队管理的核心是什么?
Use 3 in data modeling σ Eliminate outliers for data cleaning
Niuke - Huawei question bank (61~70)
Dynamics 365online applicationuser creation method change