【leetcode】1254. Number of Closed Islands
题目如下:
Given a 2D
grid
consists of0s
(land) and1s
(water). An island is a maximal 4-directionally connected group of0s
and a closed island is an island totally (all left, top, right, bottom) surrounded by1s.
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的更多相关文章
- 【LeetCode】694. Number of Distinct Islands 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【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. ...
- 【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 ...
- 【LeetCode】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
随机推荐
- JAVA SQL注入漏洞挖掘
java中sql注入主要发生在model层,黑盒测试sql注入的方法结合两点:1,异常注入后,界面有无明显的aql异常报出.2,查看数据库日志是否有脏数据注入. preparestatement方法是 ...
- 再谈循环&迭代&回溯&递归&递推这些基本概念
循环:不断重复进行某一运算.操作. 迭代:不断对前一旧值运算得到新值直到达到精度.一般用于得到近似目标值,反复循环同一运算式(函数),并且总是把前一 次运算结果反代会运算式进行下一次运算 递推:从初值 ...
- PTA(Basic Level)1037.在霍格沃茨找零钱
如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 -- 就如海格告诉哈利的:"十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一个西可,很容易.& ...
- 在Ubuntu上安装Hbase
1.下载hbase2.0.5 解压缩并改名为hbase目录,放到/usr/local下(注意权限) sudo mv ~/hbase-2.0.5 /usr/local/hbase 2.修改目录所有者 / ...
- <<C++ Primer>> 第 6 章 函数
术语表 第 6 章 函数 二义性调用(ambiguous call): 是一种编译时发生的错误,造成二义性调用的原因时在函数匹配时两个或多个函数提供的匹配一样好,编译器找不到唯一的最佳匹配. 实 ...
- 2019中山纪念中学夏令营-Day19 数论初步【GCD(最大公约数),素数相关】
关于GCD的一些定理或运用的学习: 1. 2.二进制算法求GCD 思想:使得最后的GCD没有2(提前把2提出来) 代码实现: #include <cstdio> #define int l ...
- 牛客 545A 小A与最大子段和 & CF 660F Bear and Bowling 4
大意: 给定序列$a$, 求选择一个子区间$[l,r]$, 使得$\sum\limits_{i=l}^r(i-l+1)a_i$最大. $n\le2e5, |a_i|\le 1e7$. 记$s[i]=\ ...
- bzoj 4237 稻 草 人
bzoj 这个矩形有三个限制,分别是右上角点的横纵坐标分别大于左下角废话,并且中间区域没有点.那么可以先按横坐标排序,然后枚举左边的点和右边的点匹配.为了保证复杂度,这里每次把点集一分为二,先递归处理 ...
- 转换byte(十进制)为图形(大小写字母,符号)
/** * 转换byte(十进制)为字母 */ public static String ByteToLetter (byte[] chars) { String Letter = "&qu ...
- 淘宝flexible.js的使用
首先大家最关注的怎么使用,原理不原理是后面的事 比如设计稿量来的宽度是100px 那么我们布局的时候,就这么写{width:1.3333rem},1.3333rem是由100/75算出来的,以此类推2 ...