题目描述:

自己的提交:

class Solution:
def closedIsland(self, grid: List[List[int]]) -> int:
def dfs(grid,r,c):
nr = len(grid)
nc = len(grid[0])
if r<0 or c<0 or r==nr or c==nc or grid[r][c]==1:
return
grid[r][c] = 1
dfs(grid,r-1,c)
dfs(grid,r+1,c)
dfs(grid,r,c-1)
dfs(grid,r,c+1)
if not grid:
return 0
nr = len(grid)
nc = len(grid[0])
for i in (0,nr-1):
for j in range(nc):
if grid[i][j]==0:
dfs(grid,i,j)
for i in range(nr):
for j in (0,nc-1):
if grid[i][j]==0:
dfs(grid,i,j)
num = 0
for i in range(nr):
for j in range(nc):
if grid[i][j]==0:
num += 1
dfs(grid,i,j)
return num

另:

class Solution(object):
def closedIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
n, m = len(grid), len(grid[0])
land = set((i,j)
for i in xrange(n)
for j in xrange(m)
if grid[i][j] == 0)
seen = set()
r = 0
for x in xrange(n):
for y in xrange(m):
if (x, y) in seen: continue
if (x, y) not in land: continue
q = set()
q.add((x, y))
f = True
while q:
f = f and all(0<i<n-1 and 0<j<m-1 for i, j in q)
seen.update(q)
q = set((ii, jj)
for i, j in q
for ii, jj in ((i-1, j), (i+1, j), (i, j-1), (i, j+1))
if (ii, jj) in land and (ii, jj) not in seen)
r += f
return r

leetcode-162周赛-1254-统计封闭岛屿数量的更多相关文章

  1. LEETCODE 1254 统计封闭岛屿的数目 Number of Closed Islands

    地址 https://leetcode-cn.com/contest/weekly-contest-162/problems/number-of-closed-islands/ 有一个二维矩阵 gri ...

  2. Leetcode 1254. 统计封闭岛屿的数目

    题目: 有一个二维矩阵 grid ,每个位置要么是陆地(记号为 0 )要么是水域(记号为 1 ). 我们从一块陆地出发,每次可以往上下左右 4 个方向相邻区域走,能走到的所有陆地区域,我们将其称为一座 ...

  3. 【LeetCode】200. Number of Islands 岛屿数量

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  4. [LeetCode] 305. 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 ...

  5. Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)

    Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...

  6. LeetCode 200:岛屿数量 Number of Islands

    题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. Given ...

  7. [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)

    695. 岛屿的最大面积 题目 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合.你可以假设二维矩阵的四个边缘都被 ...

  8. Leetcode题目200.岛屿数量(BFS+DFS+并查集-中等)

    题目描述: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...

  9. Java实现 LeetCode 200 岛屿数量

    200. 岛屿数量 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. ...

随机推荐

  1. 在scite编辑器中使用astyle格式化c代码

    用 vc6 很喜欢 alt+F8 的功能. 能够格式化代码. scite 没有这个功能. 今天看它的配置文件发现这么两行: command.name.0.*.cxx=Indent command.0. ...

  2. 服务器中卸载JDK

  3. Java实现按汉语拼音的排序

    public class sortByPinyin { public static void main(String[] args) { String[] arr = { "刘刘" ...

  4. spring整合hibernate之买书小测试

    spring来整合hibernate就是用spring来管理hibernate的sessionFactory和让hibernate来使用spring的声明式事务. 一:加入相应的jar包. 二:写hi ...

  5. jdbc blob插入及查询操作

    首先建一张表 create table picture( picId ) primary key not null, picName ) not null, picfile image null ) ...

  6. mysql5.6配置-my

    # mkdir -p /home/mysql/3306/{data,binlog,logs} [client] port = socket=/tmp/my3306.sock [mysql] no-au ...

  7. [CSP-S模拟测试51]题解

    错失人生中第一次AK的机会…… A.attack 支配树板子题.考场上发明成功√ 首先支配树上两点路径之间的点都是必经之点,根据这个性质我们就可以yy出建树的方法.跑拓扑,在每个点(设为$x$)即将入 ...

  8. Mamen所需要的jar包怎么生成

    Mamen所需要的jar包怎么生成 使用 mamen 难免碰到,不知道的 jar 包,不知道怎么在 pom 文件中写,分享一个网址,可以把你想要的 jar 包生成 pom 配置文件,个人感觉非常好用. ...

  9. 阻抗匹配 及 SI9000 使用

    1. 阻抗匹配 1. 波长 * 频率 = 光速(3*10^8) 2. PCB走线什么时候需要做阻抗匹配? 不主要看频率,而关键是看信号的边沿陡峭程度,即信号的上升/下降时间,一般认为如果信号的上升/下 ...

  10. expand gcc case variable length

    daniel@daniel-mint ~/vex $ bash gen.sh 0x10 0x1F case 10: case 11: case 12: case 13: case 14: case 1 ...