"""
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
"""
"""
本题与1162,542 类似
有BFS,和DFS两种做法,
解法一:BFS
我个人更习惯BFS的做法
"""
class Solution1:
def numIslands(self, grid):
if not grid or not grid[0]:
return 0
queue = []
res = 0
m = len(grid)
n = len(grid[0])
for x in range(m):
for y in range(n):
if grid[x][y] == '':
res += 1
grid[x][y] == '' #!!!找到为1的,将其变为0,并从四个方向遍历,把整个岛变为0
queue.append((x, y))
while queue:
a, b = queue.pop()
for i, j in [(a+1, b), (a-1, b), (a, b-1), (a, b+1)]:
if 0 <= i < m and 0 <= j < n and grid[i][j] == '':
grid[i][j] = ''
queue.append((i, j))
return res """
我们对每个有“1"的位置进行dfs,把和它四联通的位置全部变成“0”,这样就能把一个点推广到一个岛。
所以,我们总的进行了dfs的次数,就是总过有多少个岛的数目。
注意理解dfs函数的意义:已知当前是1,把它周围相邻的所有1全部转成0.
"""
class Solution2:
def numIslands(self, grid):
if not grid or not grid[0]:
return 0
queue = []
res = 0
m = len(grid)
n = len(grid[0])
for x in range(m):
for y in range(n):
if grid[x][y] == '':
self.dfs(grid, x, y)
res += 1
return res def dfs(self, grid, a, b):
grid[a][b] = 0
for i, j in [(a + 1, b), (a - 1, b), (a, b - 1), (a, b + 1)]:
if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] == '':
self.dfs(grid, i, j)

leetcode200 Number of Islands的更多相关文章

  1. Leetcode200. Number of Islands岛屿的个数

    给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...

  2. [Swift]LeetCode200.岛屿的个数 | 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] 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 ...

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

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

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

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

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

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

随机推荐

  1. 【SSM 】导出excel含图片

    ExprotRentUtils package com.kikyo.stat.utils; import java.awt.image.BufferedImage; import java.io.By ...

  2. FTP文件传输服务!

    一.FTP  连接及传输模式 1.控制连接:TCP 21,用于发送 FTP 命令信息2.数据连接:TCP 20,用于上传.下载数据3.数据连接的建立类型: (1)主动模式:服务器主动发起数据连接 (2 ...

  3. linux-命令行快捷方式使用

    CTRL+P 命令向上翻滚 CTRL+N  命令向下翻滚 CTRL+U 命令行中删除光标前面的所有字符 CTRL+D 命令行中删除光标后面的一个字符 CTRL+H 命令行中删除光标前面的一个字符 CT ...

  4. centos610无桌面安装openoffice

     Centos610系列配置卸载yum remove libreoffice*yum remove openoffice* 安装yum install openoffice.org-writer yu ...

  5. Matlab利用subplot绘制多个图像

    利用subplot绘制多个图像 subplot(m,n,p) subplot是将多个图画到一个平面上的函数,m是行,n是列,p是所要绘制图所在的位置 x = 0:0.1:100; sinY = sin ...

  6. DCL和DQL

    数据查询语言(DQL,Data Query Language) 主要是一些查询的sql语句. 语法 select * from test: 数据控制语言(DCL, Data Control Langu ...

  7. 吴裕雄--天生自然 JAVA开发学习:java使用Eclipel连接数据库

    1:jar可到Mysql官网下载:地址Mysql 连接jar包.:https://dev.mysql.com/downloads/connector/j/如图,在下拉列表框中选择Platform In ...

  8. 含有namespace的类型如何访问

    下图中包含的String类型,如果想要在别的文件中去访问的话: 1)需要include"ApiClient.hpp" 2)需要使用oatpp::web::client::ApiCl ...

  9. 「NOIP2017」逛公园

    传送门 Luogu 解题思路 考虑 \(\text{DP}\). 设 \(f[u][k]\) 表示从 \(u\) 到 \(n\) 走过不超过 \(Mindis(u, n) + k\) 距离的方案数. ...

  10. 【JAVA蓝桥杯】基础练习2 十六进制转十进制

    资源限制 时间限制:1.0s   内存限制:512.0MB 问题描述 从键盘输入一个不超过8位的正的十六进制数字符串,将它转换为正的十进制数后输出. 注:十六进制数中的10~15分别用大写的英文字母A ...