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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. 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 用了第一种方式, ...

  5. [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 ...

  6. Leetcode 200.岛屿的数量 - DFS、BFS

    Leetcode 200 岛屿的数量: DFS利用函数调用栈保证了检索顺序, BFS则需要自己建立队列,把待检索对象按规则入队. class Solution { // DFS解法,8ms/10.7M ...

  7. [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 ...

  8. (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 ...

  9. [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 ...

随机推荐

  1. 原生js--客户端存储的种类

    客户端存储遵循同源策略,不同的站点页面之间不可以相互读取对方的数据,但同一站点的不同页面之间可以共享存储的数据 客户端存储的种类: 1.web存储 localStorage.sessionStorag ...

  2. hdu3507 Print Article[斜率优化dp入门题]

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  3. python nose测试框架全面介绍八---接口测试中非法参数的断言

    在测接口时,会有这样的场景,输入非法的参数,校验返回的错误码及错误内容 通常做法为发请求,将错误的返回结果拿出,再进行对比匹配:但存在一个问题,需要再写错误返回分析函数,不能与之前正常发请求的函数共用 ...

  4. 通过Intent Flags ,从桌面返回到App最后Activity

    extends:http://bbs.csdn.net/topics/350269396,http://blog.csdn.net/moreevan/article/details/6788048 最 ...

  5. stylie工具轻松搞定css3抛物线动画

    自从CSS3引入了动画(transition和@keyframes,还有与之搭配的transform)之后,写动画也变的越来多越来越容易. 可是当我们遇到需要利用数学公式的复杂动画时,却一筹莫展,只能 ...

  6. iOS - 友盟集成QQ分享的AppID转换16进制的方法

    设置xcode的url scheme格式为“QQ”+腾讯QQ互联应用appId转换成十六进制(不足8位前面补0),例如“QQ41EE2B54”.生成十六进制方法:echo 'ibase=10;obas ...

  7. HTTP协议的前世今生——各版本HTTP协议对比

    HTTP协议是如今互联网与服务端技术的基石,HTTP协议的演进也从侧面反应了互联网技术的快速发展.这两天在准备一次关于HTTP1.1协议特性的技术分享过程中,顺便了解了下各版本HTTP协议的特点,在这 ...

  8. FZU 2252 Yu-Gi-Oh!(枚举+贪心)

    Problem 2252 Yu-Gi-Oh! Accept: 105    Submit: 628 Time Limit: 1000 mSec    Memory Limit : 32768 KB   ...

  9. python数据结构之哈希表

    哈希表(Hash table) 众所周知,HashMap是一个用于存储Key-Value键值对的集合,每一个键值对也叫做Entry.这些个键值对(Entry)分散存储在一个数组当中,这个数组就是Has ...

  10. 使用Intel IPT技术保护您的帐号安全

    使用Intel IPT技术保护您的帐号安全