谷歌面试题(5)
来源:互联网 发布:8080端口怎么打开 编辑:程序博客网 时间:2024/05/17 07:07
原题:
// You are given a m x n 2D grid initialized with these three possible values.// -1 - A wall or an obstacle.// 0 - A gate.// INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.// Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.// For example, given the 2D grid:// INF -1 0 INF// INF INF INF -1// INF -1 INF -1// 0 -1 INF INF// After running your function, the 2D grid should be:// 3 -1 0 1// 2 2 1 -1// 1 -1 2 -1// 0 -1 3 4
答案:
public class Solution { public void wallsAndGates(int[][] rooms) { //iterate through the matrix calling dfs on all indices that contain a zero for(int i = 0; i < rooms.length; i++) { for(int j = 0; j < rooms[0].length; j++) { if(rooms[i][j] == 0) { dfs(rooms, i, j, 0); } } } } void dfs(int[][] rooms, int i, int j, int distance) { //if you have gone out of the bounds of the array or you have run into a wall/obstacle, return // room[i][j] < distance also ensure that we do not overwrite any previously determined distance if it is shorter than our current distance if(i < 0 || i >= rooms.length || j < 0 || j >= rooms[0].length || rooms[i][j] < distance) { return; } //set current index's distance to distance rooms[i][j] = distance; //recurse on all adjacent neighbors of rooms[i][j] dfs(rooms, i + 1, j, distance + 1); dfs(rooms, i - 1, j, distance + 1); dfs(rooms, i, j + 1, distance + 1); dfs(rooms, i, j - 1, distance + 1); }}
阅读全文
0 0
- 谷歌面试题(5)
- 谷歌面试题(2)
- 谷歌面试题(3)
- 谷歌面试题(4)
- 谷歌面试题(6)
- 谷歌面试题(7)
- 谷歌面试题(8)
- 谷歌面试题(9)
- 面试题(5)
- 面试题(5)
- 面试题(5)
- ACM初赛试题(5)
- 华为机试题(5)
- 华为机试题(5)
- Android面试题(5)
- java面试题(5)
- 谷歌面试题
- 谷歌面试题
- java类集---IdentityHashMap
- 面试记录第二十二节——(lint检查)
- Swing界面中中文字体丑化的解决方案
- 批量免密登录
- vmware tools第一次安装失败后再次安装时出现的错误及解决
- 谷歌面试题(5)
- Faster R-CNN改进篇-ION/HyperNet/MS CNN
- Dynamic Time Warping(DTW) Algorithm
- @interface自定义注解的实际运用
- 谷歌面试题(6)
- Linux Shell脚本详细教程
- 有关USES_CONVERSION
- PID控制算法的C语言实现
- es5新增数组方法