LeetCode 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 Walls and Gates的更多相关文章
- [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 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] 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
原题链接在这里: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 ...
- 【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 ...
- [Swift]LeetCode286. 墙和门 $ Walls and Gates
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
随机推荐
- AIM Tech Round 3 (Div. 2)
#include <iostream> using namespace std; ]; int main() { int n, b, d; cin >> n >> ...
- hiveserver2
hiveserver2 默认绑定了ip:localhost 和 port:10000 !connect jdbc:hive2://localhost:10000 org.apache.hive.jd ...
- 《你必须知道的.NET》书中对OCP(开放封闭)原则的阐述
开放封闭原则(OCP,Open Closed Principle)是面向对象原则的核心.由于软件设计本身所追求的墓边就是封装变化,降低耦合,而开放封闭原则就是对这一目标的直接体现.(你必须知道的.NE ...
- jQuery 循环问题
$("#add2sub").click(function(){ var $sxarr=$(".add_shuxing_ul > .sx_add_bg"); ...
- HTTP Methods: GET v.s POST
HTTP works as a request-response protocol between a client and server. A web browser may be the clie ...
- [LintCode] Wiggle Sort II 扭动排序之二
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- Odoo 仓库扫码打包方案
Odoo仓库扫码的设计,前提是操作人在PC上先做好分拣单,然后根据打印出来的分拣单去仓库进行扫码打包,默认的情况下,分拣在被确认的时候会自动保留库位中已经存在的库存(已经分配批次\序列号),而 ...
- 行业集中度(Concentration Ratio)
行业集中度是决定市场结构最基本.最重要的因素,集中体现了市场的竞争和垄断程度,经常使用的集中度计量指标有:行业集中率(CRn指数).赫尔芬达尔—赫希曼指数(Herfindahl-HirschmanIn ...
- iframe自适应高度js
<iframe src="http://www.fufuok.com/" id="iframepage" name="iframepage&qu ...
- jQuery设计思想之取值和赋值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...