当前位置:网站首页>剑指 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;
}
}
边栏推荐
- IIS faked death this morning, various troubleshooting, has been solved
- Create an int type array with a length of 6. The values of the array elements are required to be between 1-30 and are assigned randomly. At the same time, the values of the required elements are diffe
- [cloud native] Devops (I): introduction to Devops and use of code tool
- PostgreSQL创建触发器的时候报错,
- SiteMesh getting started example
- First issue of JS reverse tutorial
- Strategic cooperation subquery becomes the secret weapon of Octopus web browser
- 华为HCIP-DATACOM-Core_03day
- Over 100000 words_ Ultra detailed SSM integration practice_ Manually implement permission management
- Unity shader (data type in cghlsl)
猜你喜欢

Esp8266 uses TF card and reads and writes data (based on Arduino)

華為HCIP-DATACOM-Core_03day

Oracle installation enhancements error

Network request process

战略合作|SubQuery 成为章鱼网络浏览器的秘密武器

PLC信号处理系列之开关量信号防抖FB

CSDN salary increase technology - learn about the use of several common logic controllers of JMeter

Jenkins task grouping

Sublime Text4 download the view in bower and set the shortcut key

12、 Sort
随机推荐
Addition, deletion, modification and query of ThinkPHP database
Liunx command
[cloud native] Devops (I): introduction to Devops and use of code tool
沙龙预告|GameFi 领域的瓶颈和解决方案
JMeter JDBC batch references data as input parameters (the simplest method for the whole website)
PostgreSQL创建触发器的时候报错,
浏览器中如何让视频倍速播放
如何成为一名高级数字 IC 设计工程师(5-2)理论篇:ULP 低功耗设计技术精讲(上)
Interface test API case, data and interface separation
Unity shader (learn more about vertex fragment shaders)
【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
thinkphp3.2信息泄露
【无标题】
sql 里面使用中文字符判断有问题,哪位遇到过?比如value&lt;&gt;`无`
Netease Cloud Wechat applet
【frida实战】“一行”代码教你获取WeGame平台中所有的lua脚本
Write VBA in Excel, connect to Oracle and query the contents in the database
章鱼未来之星获得25万美金奖励|章鱼加速器2022夏季创业营圆满落幕
Add new item after the outbound delivery order of SAP mm sto document is created?
JS逆向教程第一发