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

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

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

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

  4. 【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 ...

  5. [LintCode] Number of Islands 岛屿的数量

    Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...

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

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

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

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

随机推荐

  1. JSBinding + SharpKit / 原理篇:Delegate

    以 NGUI 的 UIEventListener 为例: 有一个类: using SharpKit.JavaScript; using UnityEngine; using System.Collec ...

  2. Andorid面试问题整理

    Acitivty的四中启动模式与特点. standard:默认的启动模式 singleTop:适合那种接受通知启动的页面,比如新闻客户端之类的,可能会给你推送好几次 ,但是每次都是打开同一张页面调用o ...

  3. playframework1.x的eclipse插件开源-playtools

    playtools介绍 playframework(1.2.x)是一款令人兴奋的java restful风格的web框架,使用它已经有两年多. 其中结合eclipse开发项目往往要不断进行cmd窗口切 ...

  4. 006. C#使用WMI操作远程计算机

    1. 使用WMI CIM studio 查看\root\CIMV2 所有可使用的表/字段(类/属性) , 点击下载 WMI CIM studio 2. 安装完成后打开VMI CIM studio

  5. wikioi 1075 明明的随机数

    /*============================================================ 1075 明明的随机数 题目描述 Description 明明想在学校中请 ...

  6. js 动态 activex 组件

    function writeObject(){ var obj = document.getElementById("mydelphi"); if(!obj){ var divob ...

  7. mybatis migrate常用指令

    0.制定db和配置文件 --path=xxx --env=dev 1.初始化Migrations工作目录 migrate init 2.创建数据库变更 migrate new "liyq a ...

  8. SQL Server 日期的加减函数: DATEDIFF DATEADD

    SQL Server 日期的加减函数: DATEDIFF    DATEADD DATEDIFF: 返回跨两个指定日期的日期边界数和时间边界数, 语法:DATEDIFF ( datepart , st ...

  9. JS使用百度地图API

    尚未整理: <script type="text/javascript"> var map = new BMap.Map("dituContent" ...

  10. (转)用JQuery实现Fix表头表格

    本文转载自:http://www.cnblogs.com/evlon/archive/2009/06/12/1502239.html 我的技术要点: 1.用两个表,其中一个是表头,另一个是表格做表体 ...