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. ...
随机推荐
- "%Error opening tftp://255.255.255.255/network config"
问题:服务配置错误消息(Service Configuration Error Messages) 有时,在通过Cisco IOS软件启动Cisco设备期间,会显示与这些类似的错误消息: %Error ...
- java操作nginx
一,判断程序的部署环境是nginx还是windows /** * 判断操作系统是不是windows * * @return true:是win false:是Linux */ public stati ...
- DHCP与DHCP中继原理与配置!(重点)
一 .DHCP 服务概述 0:dhcp原理: 集中的管理.分配IP地址,使client动态的获得IP地址.Gateway地址.DNS服务器地址等信息,并能够提升地址的使用率.简单来说,DHCP就是一 ...
- IIS 配置迁移
使用管理员身份运行cmd 应用程序池: # 导出所有应用程序池 %windir%\system32\inetsrv\appcmd list apppool /config /xml > c:\a ...
- RDLC 表达式设置精度
=IIf(RTrim(Parameters!u_currency.Value)="VND","F0","F2")
- Android的界面组件使用之ImageButton和ImageView,ImageSwitcher和GridView
(一)ImageButton和ImageView ImageButton与Button的功能完全相同,只是ImageButton上显示的是图像,并且每个ImageButton组件都必须指定一个id,以 ...
- 爬虫(十四):Scrapy框架(一) 初识Scrapy、第一个案例
1. Scrapy框架 Scrapy功能非常强大,爬取效率高,相关扩展组件多,可配置和可扩展程度非常高,它几乎可以应对所有反爬网站,是目前Python中使用最广泛的爬虫框架. 1.1 Scrapy介绍 ...
- docker 的实践操作
查看版本信息 [root@k8s-1 ~]# docker version Client: Version: 18.09.6 API version: 1.39 Go version: go1.10. ...
- 5 HTML脚本&字符实体&URL
HTML脚本: 用<script>标签定义客户端脚本,比如JavaScript script元素即可包含脚本语句,也可以通过src属性指向外部脚本文件 JavaScript常用于图片操作. ...
- MFC加载图片
目录 1. 自适应方法 2. 加载原图方法 1. 自适应方法 /* 自适应方法 */ CRect rect; CRect rect1; CImage image; //创建图片类 image.Load ...