LeetCode 286. Walls and Gates
原题链接在这里:https://leetcode.com/problems/walls-and-gates/
题目:
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 value231 - 1 = 2147483647
to representINF
as you may assume that the distance to a gate is less than2147483647
.
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
题解:
BFS, 先把所有gate加到que中。对于每一个从que中poll出来的gate,看四个方向是否有room, 若有,把room的值改正gate + 1, 在放回到que中.
Time Complexity: O(m*n). m = rooms.length, n = rooms[0].length. 每个点没有扫描超过两遍. Space: O(m*n).
AC Java:
- class Solution {
- public void wallsAndGates(int[][] rooms) {
- if(rooms == null || rooms.length == 0 || rooms[0].length == 0){
- return;
- }
- int m = rooms.length;
- int n = rooms[0].length;
- LinkedList<int []> que = new LinkedList<>();
- int level = 1;
- for(int i = 0; i < m; i++){
- for(int j = 0; j < n; j++){
- if(rooms[i][j] == 0){
- que.add(new int[]{i, j});
- }
- }
- }
- int [][] dirs = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
- while(!que.isEmpty()){
- int size = que.size();
- while(size-- > 0){
- int [] cur = que.poll();
- for(int [] dir : dirs){
- int x = cur[0] + dir[0];
- int y = cur[1] + dir[1];
- if(x < 0 || x >= m || y < 0 || y >= n || rooms[x][y] != Integer.MAX_VALUE){
- continue;
- }
- que.add(new int[]{x, y});
- rooms[x][y] = level;
- }
- }
- level++;
- }
- }
- }
跟上Robot Room Cleaner, Rotting Oranges, Shortest Distance from All Buildings.
LeetCode 286. Walls and Gates的更多相关文章
- [LeetCode] 286. Walls and Gates 墙和门
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- 【LeetCode】286. Walls and Gates 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 286. Walls and Gates
题目: You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an ob ...
- [LeetCode] 286. Walls and Gates_Medium tag: BFS
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings
542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...
- [Locked] Walls and Gates
Walls and Gates You are given a m x n 2D grid initialized with these three possible values. -1 - A w ...
- [LeetCode] Walls and Gates 墙和门
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- LeetCode Walls and Gates
原题链接在这里:https://leetcode.com/problems/walls-and-gates/ 题目: You are given a m x n 2D grid initialized ...
- Walls and Gates -- LeetCode
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
随机推荐
- 案例:使用logstash收集游戏服务器日志,输出到kafka消息队列中,然后存入ES
gamelogs2kafka.conf input { file { codec => plain { charset => "UTF-8" } path => ...
- springboot yml配置文件注入值
1.编写javabean: package com.example.springboot.bean; import org.springframework.boot.context.propertie ...
- 在Eclipse中使用Beyond Compare做为比较工具
1.下载org.eclipse.externaltools-Update-0.8.9.v201003051612.zip插件包 接下来,要下载Beyond Compare的插件,http://beyo ...
- using 语句(C# 参考)(转载)
using 语句 提供可确保正确使用 IDisposable对象的方便语法. 示例 下面的示例演示如何使用 using 语句. using (var font1 = new Font("Ar ...
- ASP.NET Core中如何显示[PII is hidden]的隐藏信息
有时候我们在ASP.NET Core项目运行时,发生在后台程序中的错误会将关键信息隐藏为[PII is hidden]这种占位符,如下所示: 而知道这些关键信息,有时候对我们调试程序是非常重要的.所以 ...
- C#读写调整设置UVC摄像头画面-亮度
有时,我们需要在C#代码中对摄像头的亮度进行读和写,并立即生效.如何实现呢? 建立基于SharpCamera的项目 首先,请根据之前的一篇博文 点击这里 中的说明,建立基于SharpCamera的摄像 ...
- CKEditor 4 上传图片
参考资料:Basic Configuration 直接Ctrl+v(粘贴图片)报错信息:上传文件时发生网络错误(networkError:Network error occurred during f ...
- Python基础之面向对象编程
面向对象编程 —— Object Oriented Programming 简写 OOP 01. 面向对象基本概念 我们之前学习的编程方式就是 面向过程 的 面向过程 和 面向对象,是两种不同的 编程 ...
- Spring Security 解析(三) —— 个性化认证 以及 RememberMe 实现
Spring Security 解析(三) -- 个性化认证 以及 RememberMe 实现 在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把 ...
- 【转载】 Windows系统电脑通过设备管理器查看电脑配置信息
在采购电脑或者使用电脑的过程中,有时候我们需要查看到电脑的所有设备硬件信息,此时就可以通过Windows系统自带的设备管理器界面来查看该电脑所有的设备配置信息,包括CPU型号频率.内存.硬盘型号以及容 ...