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. ...
随机推荐
- 「Luogu P1975 [国家集训队]排队」
题目大意 给出一个序列 \(h\),支持交换其中的两数,求出每一时刻的逆序对个数. 分析 求逆序对是 \(O(N\log_2N)\) 的,有 \(M\) 个操作,如果暴力求的话时间复杂度就是 \(O( ...
- centos 安装phpize
yum -y install php-devel 然后 /usr/bin/phpize
- Spring Boot 项目本地运行无异常,部署到 Linux 服务器运行报错:java.lang.ClassNotFoundException
一 背景 最近在用 Springboot 开发项目 A,引了小伙伴开发的模块 B,本地起服务,运行的好好的,等部署到服务器上,一运行就报错:Caused by: java.lang.ClassNotF ...
- Flask程序相关配置加载的三种方式
方式一:从对象中加载配置 1.定义配置类,在配置类中添加相应的配置 2.通过app.config.from_object(配置类)进行加载 代码如下: from flask import Flask ...
- 4.ORM框架的查询
创建表对应关系代码如下: from flask import Flask, render_template from flask_sqlalchemy import SQLAlchemy app=Fl ...
- SpringMVC:详述拦截器
将堆内存SpringMVC使用拦截器对请求进行拦截处理,以实现特定的功能: 具体实现: 1.自定义一个实现HandlerInterceptor接口的类并实现接口中定义的抽象方法(实现了三个方法,分别处 ...
- flask blueprints
flask blueprints 1. flask blueprints 蓝图是一种网页模板的组合,可以方便的注册到flask中. 蓝图可以在文件中声明,也可以在包中声明,一般而言推荐在包中 ...
- 【剑指Offer面试编程题】题目1361:翻转单词顺序--九度OJ
题目描述: JOBDU最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,&quo ...
- 杭电2024 C语言合法标识符
链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2024 开始真的对这题是一点头绪都没有,简直了.然后事实证明是我想多了,这题主要是把概念给弄清楚 ...
- Systemverilog for design 笔记(六)
转载请标明出处 第一章 有限状态机建模(FSM,finite state machine) 1.1. 使用枚举类型建立状态机模型 l 三过程块建模风格:三个过程块分别实现: a.状态转换(al ...