题目如下:

Given a 2D grid consists of 0s (land) and 1s (water).  An island is a maximal 4-directionally connected group of 0s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s.

Return the number of closed islands.

Example 1:

Input: grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
Output: 2
Explanation:
Islands in gray are closed because they are completely surrounded by water (group of 1s).

Example 2:

Input: grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
Output: 1

Example 3:

Input: grid = [[1,1,1,1,1,1,1],
  [1,0,0,0,0,0,1],
  [1,0,1,1,1,0,1],
  [1,0,1,0,1,0,1],
  [1,0,1,1,1,0,1],
  [1,0,0,0,0,0,1],
[1,1,1,1,1,1,1]]
Output: 2 

Constraints:

  • 1 <= grid.length, grid[0].length <= 100
  • 0 <= grid[i][j] <=1

解题思路:典型的BFS/DFS的场景,没什么好说的。

代码如下:

class Solution(object):
def closedIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
visit = [[0] * len(grid[0]) for _ in grid]
res = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1 or visit[i][j] == 1:continue
queue = [(i,j)]
visit[i][j] = 1
closed = True
while len(queue) > 0:
x,y = queue.pop(0)
if x == 0 or x == len(grid) - 1 or y == 0 or y == len(grid[0]) - 1:
closed = False
direction = [(0,1),(0,-1),(1,0),(-1,0)]
for (x1,y1) in direction:
if x + x1 >= 0 and x + x1 < len(grid) and y + y1 >= 0 \
and y + y1 < len(grid[0]) and visit[x+x1][y+y1] == 0\
and grid[x+x1][y+y1] == 0:
queue.append((x+x1,y+y1))
visit[x+x1][y+y1] = 1
if closed:res += 1
return res

【leetcode】1254. Number of Closed Islands的更多相关文章

  1. 【LeetCode】694. Number of Distinct Islands 解题报告 (C++)

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

  2. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  3. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  4. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  5. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

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

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

  7. 【LeetCode】200. Number of Islands (2 solutions)

    Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...

  8. 【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 s ...

  9. 【LeetCode】476. Number Complement (java实现)

    原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...

随机推荐

  1. Interceptors - 拦截器

    1.概述 Flume有能力在运行阶段修改/删除Event,这是通过拦截器(Interceptors)来实现的. 拦截器需要实现org.apache.flume.interceptor.Intercep ...

  2. SolidWorks学习笔记6抽壳,加强筋,扫描,放样

    抽壳 概念:移除一个或者多个面,然后将其余的模型外表面向内或者向外偏移相等或者不等的距离 针对不同面设置不同厚度 方向参考 有实体的一侧是内测, 没有实体的一侧是外侧 顺序 先圆角再抽壳 加强筋. 点 ...

  3. Spring实现构造注入

    Spring通过setter访问器实现对属性的赋值,这种做法称为设值注入:Spring还提供了通过构造方法赋值的能力,称为构造注入.使用设值注入时,Spring通过JavaBean的无参构造方法实例化 ...

  4. FFmpeg4.0笔记:本地媒体文件解码、帧格式转换、重采样、编码、封装、转封装、avio、硬解码等例子

    Github https://github.com/gongluck/FFmpeg4.0-study/blob/master/official%20example/my_example.cpp #in ...

  5. CSS3点击波浪按钮特效

    在线演示 本地下载

  6. Codeforces 1194D. 1-2-K Game

    传送门 先考虑只能走 $1,2$ 步的情况,设 $p[i]$ 表示当 $n=i$ 时先手是否必胜 自己手玩一下发现 $p$ 就是 $011011011...011$ 这样循环(下标从 $0$ 开始,其 ...

  7. 数据结构(三) 树和二叉树,以及Huffman树

    三.树和二叉树 1.树 2.二叉树 3.遍历二叉树和线索二叉树 4.赫夫曼树及应用 树和二叉树 树状结构是一种常用的非线性结构,元素之间有分支和层次关系,除了树根元素无前驱外,其它元素都有唯一前驱. ...

  8. EJS学习(二)之语法规则上

    标签含义 <% %> :'脚本' 标签,用于流程控制,无输出即直接使用JavaScript语言. <%= %>:转义输出数据到模板(输出是转义 HTML 标签)即在后端定义的变 ...

  9. openlayers之地图截图

    方法1 //this.map._this为初始化地图对象 this.map._this.once('postcompose', function (event) { var canvas = even ...

  10. 爬虫框架Scrapy 的使用

    一.官网链接 https://docs.scrapy.org/en/latest/topics/architecture.html 二.Scrapy 需要安装的包 #Windows平台 # pip3 ...