[LeetCode] 200. Number of Islands_ Medium tag: BFS
Given a 2d grid map of '1'
s (land) and '0'
s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
Input:
11110
11010
11000
00000 Output: 1
Example 2:
Input:
11000
11000
00100
00011 Output: 3 这个题目也是经典的BFS, 思路就是将2D array扫一遍, 然后每次如果扫到的元素是'1' and not in visited, ans += 1, 并且建一个queue, 将所有跟它相邻的4个元素如果是'1' 并且没有visited过的,
append进入queue, mark 为visited, recursively直到把所有4个方向的'1' 的元素都mark为visited, 然后一直把整个array扫完, 然后return ans. 1. Constraints 1) array can be empty or col == 0 , return 0
2) the element in array will only be '1' or '0' 2. Ideas BFS: T: O(m*n) S: O(m*n) 3. Code
class Solution:
def numIslands(self, grid):
ans,visited = 0, set()
if not grid or len(grid[0]) == 0: return ans
lr, lc = len(grid), len(grid[0])
for i in range(lr):
for j in range(lc):
if grid[i][j] == '' and (i,j) not in visited:
ans += 1
visited.add((i,j))
queue, dirs = collections.deque([(i,j)]), [(0,1), (0,-1), (-1,0), (1,0)]
while queue:
r, c = queue.popleft()
for d1, d2 in dirs:
nr, nc = r + d1, c + d2
if 0<= nr < lr and 0<= nc < lc and grid[nr][nc] == '' and (nr, nc) not in visited:
queue.append((nr, nc))
visited.add((nr,nc))
return ans
4. test cases
1) [], [[]]
2) [[1]]
3)
Input:
11110
11010
11000
00000 Output: 1
4)
Input:
11000
11000
00100
00011 Output: 3
[LeetCode] 200. Number of Islands_ Medium tag: BFS的更多相关文章
- [LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- [LeetCode] 103. Binary Tree Zigzag Level Order Traversal _ Medium tag: BFS
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
- [LeetCode] 200. Number of Islands 岛屿的数量
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Leetcode 200.岛屿的数量 - DFS、BFS
Leetcode 200 岛屿的数量: DFS利用函数调用栈保证了检索顺序, BFS则需要自己建立队列,把待检索对象按规则入队. class Solution { // DFS解法,8ms/10.7M ...
- [leetcode]200. Number of Islands岛屿个数
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- (BFS/DFS) leetcode 200. Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [LeetCode] 261. Graph Valid Tree _ Medium tag: BFS
Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), w ...
随机推荐
- 【python3】基于 qq邮箱的邮件发送
脚本内容: #!/usr/bin/python3 # -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText ...
- swagger环境搭建
下面所用工具下载 http://editor.swagger.io/#/ demo 一.安装 swagger editor 说明:安装swagger前需要安装node工具 工具安装 ...
- Android Studio 第一次启动配置
第一次启动AS前,为了避免重新下载新版本的SDK 操作如下: AS启动前,请先将bin目录的idea.properties文件中增加一行:disable.android.first.run=true ...
- JUnit(>4.0)@BeforeClass、@Before、@Test、@After、@AfterClass、@Ignore
JUnit 4 开始使用 Java 5 中的注解(annotation),常用的几个 annotation 介绍: @BeforeClass:针对所有测试,只执行一次,且必须为static void ...
- Protocol Buffers java
Protocol Buffers https://developers.google.cn/protocol-buffers/ 一. 例 addressbook.proto. syntax = &qu ...
- Centos7.0 配置docker 镜像加速
在Docker Hub官网上注册帐号,即可下载使用仓库里的全部的docker镜像.而因为网络原因,国内的开发者没办法流畅的下载镜像,经常会出现下载中断的错误.解决方法就是使用国内的容器Hub加速服务, ...
- Unity3D笔记 愤怒的小鸟<三> 实现Play界面2
前言:在Play页面中给Play页面添加一个“开始游戏”和“退出游戏”按钮顺便再来一个背景音乐 添加按钮可以是GUI.Button(),也可以是GUILayout.Button():给图片添加按钮可以 ...
- intellij idea如何快速格式化代码
选中代码,一键格式化代碼: Ctrl+Alt+L
- Elasticsearch 索引、更新、删除文档
一.Elasticsearch 索引(新建)一个文档的命令: curl XPUT ' http://localhost:9200/test_es_order_index/test_es_order_t ...
- Vim多行编辑
vim编辑文档有时候需要多行同时插入或者删除,比如多行加注释应该怎么操作 vim进了多行编辑模式:<ESC>之后按CTRL+V进入visual block模式(列编辑). 光标移到某行行首 ...