【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 ...
随机推荐
- CMMI将能力成熟度分为5个级别
CMMI将能力成熟度分为5个级别(初始级,已管理级,已定义级,量化管理级,优化级) . 初始级 此时软件过程是无序的,有时甚至是混乱的,对过程几乎没有定义,成功取决于个人努力.管理是反应式的. .可管 ...
- Python 列表(List)
列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现. 列表的数据项不需要具有相同的类型. 一.列表定义 用逗号分隔不同的数据项使用方括号括起来. >>> li ...
- Java中对象和引用的理解
偶然想起Java中对象和引用的基本概念,为了加深下对此的理解和认识,特地整理一下相关的知识点,通过具体实例从两者的概念和区别两方面去更形象的认识理解,再去记忆. 一.对象和引用的概念: 在Java中万 ...
- [Bzoj1001][BeiJing2006]狼抓兔子(网络流/对偶图)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 看到大佬们都是对偶图过的,写了个最大流水过去了QAQ,网络流的无向图直接建双向边( ...
- 微信小程序,预览在开发工具上显示正常,手机预览二维码报request->fail错误,打开手机的调试功能又正常。
这里错误很明显是属于网址错误,开发工具和手机调试都能走request->success: 唯独常规模式下无法显示. 最开始调试过很多方法,没找出原因.最后到小程序开发设置才发现,自己未配置服务器 ...
- Linux下用OTL操作MySql(包含自己封装的类库及演示样例代码下载)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/ClamReason/article/details/23971805 首先重点推荐介绍otl介绍及使 ...
- linux增加swap大小
参考自:https://blog.csdn.net/ssrmygod/article/details/70157716 我在centos6.5上照着操作成功了首先查一下目前swap的大小: [root ...
- 使用Jsoup爬取网站图片
package com.test.pic.crawler; import java.io.File; import java.io.FileOutputStream; import java.io.I ...
- loj 2336「JOI 2017 Final」绳
loj 首先,所有位置最多被染色一次,因为要染多次的话,还不如一开始就染成最终的颜色.并且你可以一开始就染好色 因为最终长度为2,那么如果染完后这个序列可以被折完,那么首先最多只有两种颜色,还有就是要 ...
- thinkphp3.2.3 自定义路由实践
使用了很久的tp3,却没发现还有这么可玩性的功能. 官方文档:要使用路由功能,前提是你的URL支持PATH_INFO(或者兼容URL模式也可以,采用普通URL模式的情况下不支持路由功能),并且在应用( ...