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 ...
随机推荐
- ascii codec can't decode byte 0xe8 in position 0:ordinal not in range(128)
问题描述:一个在Django框架下使用Python编写的定时更新项目,在一个Linux系统下运行没有问题,在另外一台Linux系统下测试,报如下错误: ascii codec can't decode ...
- word2010插入奇数页 使奇偶页不同的问题
word2010版本,设计奇偶页不同时,遇到一个问题,就是在第一章最后插入分节符——奇数页时,会导致整个奇数页都为1.原因:①首先每一章的末尾都应该有分页符或者奇数页等分节符来分开每一章:②点击页码可 ...
- PgSQL · 特性分析 · 谈谈checkpoint的调度
在PG的众多参数中,参数checkpoint相关的几个参数颇为神秘.这些参数与checkpoint的调度有关,对系统的稳定性还是比较重要的,下面我们为大家解析一下,这要先从PG的数据同步机制谈起. P ...
- Linux静态库和共享库
1.什么是静态库静态库类似windows中的静态lib 关于windows中的静态lib,可参考 Windows动态链接库DLL 特点:包含函数代码声明和实现,链接后所有代码都嵌入到宿主程序中. 只在 ...
- 关于ftp操作内容记录
ftp问题解决: http://www.linuxdiyf.com/viewarticle.php?id=272088 IPTABLES_MODULES="ip_nat_ftp ip_con ...
- linux服务之rsyslog
日志片断分析 systemd:服务报出来的信息kvm:内核模块kvm报出来的信息kernel: tun: 内核的tun模块报出来的信息kernel: br0: 内核的br0模块报出来的信息kernel ...
- JS 阻止浏览器默认行为和冒泡事件
JS 冒泡事件 首先讲解一下js中preventDefault和stopPropagation两个方法的区别: preventDefault方法的起什么作用呢?我们知道比如<a href=& ...
- apache配置weblogic部署集群,多节点的项目和单节点项目并存 负载均衡
开启模块如下: LoadModule weblogic_module modules/mod_wl_22.so LoadModule lbmethod_byrequests_module module ...
- intellij idea +maven4+springmvc4搭建
0.淘宝mave培训PPT http://www.open-open.com/doc/view/4058453cde4b429c82ff2807d8aa81f0 1.intellij创建空的maven ...
- Windows 8.1 系统上用Oracle VM VirtualBox 安装windows 2008 R2 SP1 的虚拟机 出现 Error Code: 0x000000C4
Windows 8.1 本来可以安装Hyper-v来安装虚拟机,但是我现在需要使用Oracle VM VirtualBox来安装虚拟机, 所以必须先卸载Hyper-v VirtualBox 安装的虚拟 ...