[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 ...
随机推荐
- H5 password自动记录取消
最近完成一个项目时需要取消谷歌浏览器的密码自动填充功能,为了用户方便,大多浏览器都有保存某个网站的密码并在后面再打开这个网站且需要输入密码的时候自动填充.这个功能是方便,但是我们有时候不需要使用这个功 ...
- python tkinter教程-事件绑定
一个Tkinter主要跑在mainloop进程里.Events可能来自多个地方,比如按键,鼠标,或是系统事件. Tkinter提供了丰富的方法来处理这些事件.对于每一个控件Widget,你都可以为其绑 ...
- [原]linux下将网卡设置为混杂模式
设置为混杂模式ifconfig eth2 promisc取消设置ifconfig eth2 -promisc ------------------------------------------ 下面 ...
- ab压测工具
在学习ab工具之前,我们需了解几个关于压力测试的概念 吞吐率(Requests per second)概念:服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求 ...
- SPOJ1007 VLATTICE - Visible Lattice Points
VLATTICE - Visible Lattice Points no tags Consider a N*N*N lattice. One corner is at (0,0,0) and th ...
- 数组和字符串的基础题目学习(EPI)
学习的速度有些慢,脑袋转动的频率有些不是很高.不过今天的效率我觉得还是可以,应该不能称效率吧,就是整个感觉不错,感觉自己补充了很多的知识.其实G家和F家败了之后不知道看看算法题对接下来的找工作帮助是否 ...
- 检查mono兼容性的工具MOAM
mono的迁移工具,可以帮助我们从windows平台迁移到Linux平台,可以用来检测特定的.net的dll或exe程序对mono的兼容性,并能够给出不兼容的方法 项目地址 MoMA 项目介绍 MoM ...
- ASP.NET Session 简单超实用使用总结
一.概述 Session用于存储特定的用户会话所需的信息 . Session对象的引入是为了弥补HTTP协议的不足,HTTP协议是一种无状态的协议. Session中文是“会话”的意思,在ASP.NE ...
- [工具] CuteMarkEd
CuteMarkEd 是一款开源免费的.支持代码高亮的.朴素的 Markdown 本地编辑器,支持 Windows.Linux. 就因为程序员喜欢用,然后就拼命的开发 Markdown 编辑器么?青小 ...
- jenkins中windows节点设置开机自启动slave-agent
做web UI自动化时,为了提高效率,用了多台windows节点来跑自动化,但slave-agent每次在关机后都得手工启动,麻烦,网上看到了一系列说启动任务中,感觉还是不考虑,这里使用windows ...