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 ...
随机推荐
- Wireshark-BPF过滤规则
设置过滤规则就是让网络设备只是捕获我们感兴趣的网络数据包,如果没有设置过滤规则,即上面的 filter_app 是空字符串,那么网络设备就捕获所有类型的数据包,否则只是捕获过滤规则设置的数据包,此时过 ...
- rac 11g_第二个节点重启后无法启动实例:磁盘组dismount问题
原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明以下出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...
- CSS Position 定位属性介绍
1.介绍 1.1 说明 Position 属性:规定元素的定位类型.即元素脱离文档流的布局,在页面的任意位置显示. 1.2 主要的值 ①absolute :绝对定位:脱离文档流的布局,遗留下来的空间由 ...
- 发现PDF Transformer+转换的图像字体小了如何处理
ABBYY PDF Transformer+转换的原始图像字体太小怎么办?为了获得最佳文本识别效果,请用较高的分辨率扫描用极小字体打印的文档,否则很容易在转换识别时出错.下面小编就给大家讲讲该怎么解决 ...
- 输出n行杨辉三角数
/*===================================== 输出n行杨辉三角数 输入n,n是1-100之间的整数 ================================= ...
- Linux 下SVN自动更新
1.找到svn的所在目录: 我的目录在/usr/local/svn/myproject 2.新建post-commit vim hooks/post-commit #!/bin/shexport LA ...
- 3 TKinter设置宽高及背景色
代码示例 #!/usr/bin/env python # _*_ coding:utf-8 _*_ from Tkinter import * root = Tk() B1 = Button(root ...
- react-redux知识点
1.定义组件: 2.定义action: 3.定义Reducer:reducer1(state,action): 4.定义store:store(reducer1): 5.定义mapStateToPro ...
- js下的sleep实现
function sleep(d){ for(var t = Date.now();Date.now() - t <= d;); } sleep(5000); //当前方法暂停5秒
- Java小陷阱
基本数据类型与字符串的连接 在Java中,+不仅可作为加法运算符使用,还可作为字符串连接运算符使用. 当把任何基本数据类型的值与字符串值进行连接运算时,基本类型的值将自动类型转换为字符串类型. pub ...