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 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的更多相关文章
- Leetcode200. Number of Islands岛屿的个数
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
- [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 ...
- [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. ...
随机推荐
- leetcode 0216
目录 ✅ 893. 特殊等价字符串组 描述 解答 cpp py ✅ 811. 子域名访问计数 描述 解答 cpp py ✅ 509. 斐波那契数 描述 解答 cpp py ✅ 521. 最长特殊序列 ...
- BGR 与 HSV 模式的转换规则
HSV模式中的H.S.V分别表示色调.饱和度.亮度 RGB转化到HSV的算法:max=max(R,G,B) min=min(R,G,B) if R = max, H = (G-B)/(max-min) ...
- slice 、 substr 、replace
slice( 参数1 [,参数2] ) (注意不要让[参数1]下标越过[参数2]下标,否则会得到空字符串,且[参数2]是不包含在截取范围内的) 参数1:截取字符的[起始下标]. 值为正 ...
- 第七届蓝桥杯javaB组真题解析-抽签(第五题)
题目 /* 抽签 X星球要派出一个5人组成的观察团前往W星. 其中: A国最多可以派出4人. B国最多可以派出2人. C国最多可以派出2人. .... 那么最终派往W星的观察团会有多少种国别的不同组合 ...
- GlusterFS分布式文件系统概述
一.GlusterFS概述 GlusterFS是一个开源的分布式文件系统,同时也是Scale-Out存储解决方案Gluster的核心,在存储数据方面有强大的横向扩展能力,通过扩展不同的节点可以支持PB ...
- ios 物流时间轴,自动匹配电话号码,可点击拨打
http://www.code4app.com/thread-27587-1-1.html 资讯时间轴(折叠/展开) http://www.code4app.com/thread-32358-1-1. ...
- UIScrollView学习指南
--前言 笔者结合自己的工作经验,梳理关于UIScrollView究竟需要掌握哪些知识才算是一个好手.至于具体的实施方案,网上资源很多,自行了解吧. --正文 1.涉及到滚动和放大的功能,优先考虑使用 ...
- selenium webdriver 屏幕滚动
//山寨法 String setscroll = "document.documentElement.scrollTop=100"; JavascriptExecutor jse= ...
- PAT T1022 Werewolf
暴力搜索加剪枝~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; bool visit[maxn]; vector& ...
- 十一 三种Struts2的数据封装方式,封装页面传递的数据
Struts2的数据封装:Struts2是一个web层框架,框架是软件的半成品.提供了数据封装的基本功能. 注:Struts2底层(核心过滤器里面的默认栈里面的拦截器,具体见struts-defaul ...