【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 ...
随机推荐
- java中怎么调用python 脚本
调用方法: import java.io.BufferedReader; import java.io.InputStreamReader; public class PythonInvoke { p ...
- SpringSecurity 配置
SpringSecurity+JWT https://www.jianshu.com/p/5b9f1f4de88d https://blog.csdn.net/qq_35494808/article/ ...
- CDH6.2的配置
访问node1: 192.168.56.11:7180 Username: admin Password: admin#进入欢迎界面 Welcome--Accept License 选免费版 Add ...
- 【转贴】使用sar进行性能分析
使用sar进行性能分析 https://www.cnblogs.com/bangerlee/articles/2545747.html 很早之前就看过 但是自己一直没用过.. 2012-06-12 0 ...
- mysql下的sqlmode详解
转自:https://www.cnblogs.com/Zender/p/8270833.html 阅读目录 一,sql_mode值的含义 二,ANSI模式 三,STRICT_TRANS_TABLES模 ...
- 四则运算计算器的微信小程序_1 界面
主界面wxml文件: page{ height:100%; } .content{ min-height:100%; display:flex; flex-direction:column; alig ...
- [BZOJ 3117] [NOI1999]内存分配(STL)
[BZOJ 3117] [NOI1999]内存分配(STL) 题面 内存是计算机重要的资源之一,程序运行的过程中必须对内存进行分配. 经典的内存分配过程是这样进行的: 1.内存以内存单元为基本单位,每 ...
- MySQL性能优化(四):SQL优化
原文:MySQL性能优化(四):SQL优化 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/ ...
- EJS学习(三)之语法规则中
⚠️实例均结合node,也就是AMD规范版本 ejs中使用render()表示渲染文本 接收三个参数:模版字符串.data.options,返回一个字符串 const ejs = require('e ...
- uoj #46[清华集训2014]玄学
uoj 因为询问是关于一段连续区间内的操作的,所以对操作构建线段树,这里每个点维护若干个不交的区间,每个区间\((l,r,a,b)\)表示区间\([l,r]\)内的数要变成\(ax+b\) 每次把新操 ...