【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: 1Example 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: 2Constraints:
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 ...
随机推荐
- python 爬虫--下载图片,下载音乐
#下载图片 imgUrl='http://www.pptbz.com/pptpic/UploadFiles_6909/201211/2012111719294197.jpg' r=requests.g ...
- springBoot中tomcat默认端口修改
springboot在启动tomcat的默认端口是8080,在实际开发中,应客户要求必须使用80端口. 研究springboot后发现有两种方式可以实现修改tomcat的端口 第一.直接修改appli ...
- # 「NOIP2010」关押罪犯(二分图染色+二分答案)
「NOIP2010」关押罪犯(二分图染色+二分答案) 洛谷 P1525 描述:n个罪犯(1-N),两个罪犯之间的仇恨值为c,m对仇恨值,求怎么分配使得两件监狱的最大仇恨值最小. 思路:使最大xxx最小 ...
- [NAIPC2016]Jewel Thief(决策单调性+分治)
[NAIPC2016]Jewel Thief(决策单调性+分治) 题面 原题提交地址(题目编号H) 原题面下载地址 有\(n\)个物品,每个物品有一个体积\(w_i\)和价值\(v_i\),现在要求对 ...
- predis操作redis方法大全
//连接redis $redis = new Redis(); $redis->connect("127.0.0.1", 6379); //普通set/get操作 $redi ...
- python打印带颜色的字体
在python开发的过程中,经常会遇到需要打印各种信息.海量的信息堆砌在控制台中,就会导致信息都混在一起,降低了重要信息的可读性.这时候,如果能给重要的信息加上字体颜色,那么就会更加方便用户阅读了. ...
- 分布式唯一ID生成器Twitter
分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的. 有些时候我们希望能使用一种简单一 ...
- 03 Redis发布与订阅
以qq群的公告,单个发布者,多个收听者为例 发布/订阅 实验 发布订阅的命令 PUBLISH channel msg 将信息 message 发送到指定的频道 channel SUBSCRIBE ch ...
- 淘宝flexible.js的使用
首先大家最关注的怎么使用,原理不原理是后面的事 比如设计稿量来的宽度是100px 那么我们布局的时候,就这么写{width:1.3333rem},1.3333rem是由100/75算出来的,以此类推2 ...
- git 基本命令操作
配置 Git 的相关参数. Git 一共有3个配置文件: 1. 仓库级的配置文件:在仓库的 .git/.gitconfig,该配置文件只对所在的仓库有效.2. 全局配置文件:Mac 系统在 ~/.gi ...