"""
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. C++11特性中的to_string

    写在最前面,本文摘录于柳神笔记 to_string 的头⽂件是 #include , to_string 最常⽤的就是把⼀个 int 型变量或者⼀个数字转化 为 string 类型的变量,当然也可以转 ...

  2. Jmeter_用户定义的变量

    1.线程组->添加->配置原件->用户定义的变量 2.自定义变量引用: ${ }

  3. kafka 分区

    1. 默认存储 /tmp/kafka-logs test-0 0:分区数

  4. 安装go和goland

    1.建议去go语言中文网下载,网址:https://studygolang.com/dl ,下图是下载页面及包介绍 2.Windows版安装 3.在cmd命令行窗口输入“go version”可以查看 ...

  5. webstrom激活码

    转自https://www.cnblogs.com/mahmud/p/11847825.html 812LFWMRSH-eyJsaWNlbnNlSWQiOiI4MTJMRldNUlNIIiwibGlj ...

  6. 二十二、mysql 执行计划与存储引擎

    1.explain(执行计划)中涉及的各字段理解1.1) select_type列的取值及含义:    SIMPLE                 :简单的SELECT语句(不包括UNION操作或子 ...

  7. 启动named服务报错!

    安装及配置bind服务程序: yum -y install bind 三个关键文件: 主配置文件(/etc/named.conf) 区域配置文件(/etc/named.rfc1912.zones) 数 ...

  8. Linux 命令中 find 和 xargs 命令的用法

    find 命令(一) find 命令主要作用是沿着文件层次结构向下遍历,匹配符合条件的文件,并执行相应的操作.Linux 下 find 命令提供了相当多的查找条件,功能很强大,对应的学习难度也比较大. ...

  9. ThinkPHP6源码分析之应用初始化

    ThinkPHP6 源码分析之应用初始化 官方群点击此处. App Construct 先来看看在 __construct 中做了什么,基本任何框架都会在这里做一些基本的操作,也就是从这里开始延伸出去 ...

  10. Python - python里有类似Java的接口(interface)吗?

    参考 https://stackoverflow.com/questions/2124190/how-do-i-implement-interfaces-in-python https://stack ...