Number of Islands
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:
11110
11010
11000
00000
Answer: 1
Example 2:
11000
11000
00100
00011
Answer: 3
DFS 遍历,将1周围的1都变为0,最后数下有多少个1即可。
class Solution(object):
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
if not len(grid):
return 0
res = 0
m,n = len(grid),len(grid[0])
for i in range(m):
for j in range(n):
if grid[i][j]=='':
res += 1
self.dfs(grid,i,j,m,n)
return res def dfs(self,grid,i,j,m,n):
if i<0 or i>=m or j<0 or j>=n: return
if grid[i][j]=='':
grid[i][j]=''
dz = zip([1,0,-1,0],[0,1,0,-1])
for px,py in dz:
self.dfs(grid,i+px,j+py,m,n)
Number of Islands的更多相关文章
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LeetCode] 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. 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】Number of Islands(middle)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [LintCode] Number of Islands 岛屿的数量
Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...
- [LeetCode] Number of Islands II
Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may pe ...
- [leetcode] Number of Islands
Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...
- Java for 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: Number of Islands II && Summary of Union Find
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
随机推荐
- Articles Every Programmer Must Read
http://javarevisited.blogspot.sg/2014/05/10-articles-every-programmer-must-read.html Being a Java pr ...
- c#判断特殊字符?
, ).Select(c => (char)c).Where(c => char.IsSymbol(c) || char.IsPunctuation(c)).ToArray()); Deb ...
- PHP使用mail()函数发送邮件流程以及注意事项
1. 配置 php.ini sendmail_path = /usr/sbin/sendmail -f yourname@sth.com -t -i 2. 使用函数 mail($to, $subjec ...
- ASP.NET 各种缓存
原文地址:http://www.cnblogs.com/ltp/archive/2009/06/30/1514311.html 有时候总听到网友说网站运行好慢,不知如何是好:有时候也总见到一些朋友 ...
- 虚拟化之vmware-vcenter
VMware ESXi.VMware vCenter Server 和 vSphere Client,它们分别是 vSphere 的虚拟化层.管理层和接口层. vcenter server 插件和vc ...
- Why we made vorlon.js and how to use it to debug your JavaScript remotely
Vorlon.js is powered by node.JS, socket.io, and late-night coffee. I would like to share with you wh ...
- Tkinter颜色方案举例
效果图: 示例代码: # _*_ coding: utf-8 _*_ from Tkinter import * colors = '''#FFB6C1 LightPink 浅粉红 ,#FFC0CB ...
- 共享锁【S锁】 排他锁【X锁】
排它锁又称为写锁((eXclusive lock,简记为X锁)),若事务T对数据对象A加上X锁,则只允许T读取和修改A,其它任何事务都不能再对A加任何类型的锁,直到T释放A上的锁.它防止任何其它事务获 ...
- spring mvc 利用匿名内部类构建返回json对象
@RequestMapping(value = "/order/findOrderByIdVague/{noId}.json", method = {RequestMethod.G ...
- Pop Sequence
题目来源:PTA02-线性结构3 Pop Sequence (25分) Question:Given a stack which can keep M numbers at most. Push ...