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:
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的更多相关文章
- [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. ...
- 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 ...
- 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 ...
随机推荐
- Unity3D研究院之Android同步方法读取streamingAssets
版本Unity5.3.3 Android 小米pad1 首先非常感谢 @守着阳光 同学在下面的留言.让我解决了一个大的谜团.. 开始我知道 StreamingAssets 路径是这个 path = & ...
- share point 读取 metadata
private static void syncMetaData() { var siteUrl = @"http://..."; using (var site = new SP ...
- XML 命名空间(XML Namespaces)
XML 应用程序 XML CDATA XML 命名空间提供避免元素命名冲突的方法. 命名冲突 在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突. 这个 X ...
- CSS3盒模型之box-sizing
这些天在做一个手机端的页面,遇到了一些问题!首当其冲的就是盒子的溢出问题!大家都知道,手机的尺寸各异,各种型号的手机多得能闪瞎你们的眼睛,为了能 让这些设置更好的浏览我们的页面,我们已经不能固定页面的 ...
- SaaS、PaaS和IaaS
•SaaS(软件即服务) •PaaS(平台即服务) •IaaS(基础架构即服务)
- linux知识点
通过gui来使用通过api来使用通过cli来使用通过tui来使用 进程不在,但tcp连接还一直存在的解决办法--tcpkill命令 http://www.centoscn.com/CentOS/Int ...
- Sql2008 全文索引应用(错误7625)
在SQL Server 中提供了一种名为全文索引的技术,可以大大提高从长字符串里搜索数 据的速度,不用在用LIKE这样低效率的模糊查询了. 下面简明的介绍如何使用Sql2008 全文索引 一.检查服务 ...
- python函数: 内置函数
forthttp://blog.csdn.net/pipisorry/article/details/44755423 Python内置函数 Python内置(built-in)函数随着python解 ...
- 初识MariaDB存储引擎
在看MariaDB的存储引擎之前,可以先了解MySQL存储引擎. MySQL常用的存储引擎: MyISAM存储引擎:是MySQL的默认存储引擎.MyISAM不支持事务.也不支持外键,但其访问速度快,对 ...
- ORA-12545:因目标主机或对象不存在,连接失败!
错误原因是配置错误主机名 解决: 1. 搜索你自己安装的Oracle路径,找到这俩个文件 tnsnames.ora 和 listener.ora,修改这两个文件,修改HOST=自己的主机名 我的路径如 ...