题目如下:

Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land)

A move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid.

Return the number of land squares in the grid for which we cannot walk off the boundary of the grid in any number of moves.

Example 1:

Input: [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output: 3
Explanation:
There are three 1s that are enclosed by 0s, and one 1 that isn't enclosed because its on the boundary.

Example 2:

Input: [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output: 0
Explanation:
All 1s are either on the boundary or can reach the boundary.

Note:

  1. 1 <= A.length <= 500
  2. 1 <= A[i].length <= 500
  3. 0 <= A[i][j] <= 1
  4. All rows have the same size.

解题思路:本题和以前的岛屿问题是一样的,用DFS/BFS就好了。

代码如下:

class Solution(object):
def numEnclaves(self, A):
"""
:type A: List[List[int]]
:rtype: int
"""
direction = [(1,0),(-1,0),(0,1),(0,-1)]
count_0 = 0
count_2 = 0
for i in range(len(A)):
for j in range(len(A[i])):
if A[i][j] == 0:
count_0 += 1
continue
elif (i == 0 or j == 0 or i == len(A) - 1 or j == len(A[0])-1) and A[i][j] == 1:
A[i][j] = 2
queue = [(i,j)]
count_2 += 1
while len(queue) > 0:
a,b = queue.pop(0)
for (x,y) in direction:
if (x + a >= 0) and (x+ a < len(A)) and (y + b >= 0) and (y+b < len(A[i])) and A[x+a][y+b] == 1:
A[x + a][y + b] = 2
count_2 += 1
queue.append((x+a,y+b))
#print A
return len(A) * len(A[0]) - count_0 - count_2

【leetcode】1020. Number of Enclaves的更多相关文章

  1. 【LeetCode】Largest Number 解题报告

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

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

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

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

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

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

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

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

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

  6. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

  7. 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...

  8. 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)

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

  9. 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)

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

随机推荐

  1. Elasticsearch6.5安装&&常见问题与答案解释

    ElasticSearch是一个用Java开发的基于Lucene的搜索服务器.它可以提供一个分布式多用户能力的全文搜索引擎,基于RESTfulweb接口.现阶段它主要为Apache许可条款下的开放源码 ...

  2. VS2015发布web服务

    一.IIS中 ①添加网站 二.VS2015 ①右键解决方案→发布: ②自定义,设置配置文件名称: ③ ④发布     三.IIS中浏览(图片的ip地址是自己,上面的ip是截图别人的,所以不一样)

  3. HDU6702 ^&^(思维)

    HDU6702 ^&^ 目标为 \((A \oplus C) \& (B \oplus C) = 0\) ,易得: \(A \& B=0\) 时:\(C = 1\) . \(A ...

  4. UVA 111 历史考试

    题目描述:最长公共子序列的变形 题目序列中第i项是学生给第i号历史事件排出的序号,另外还给出了第i号历史事件的正确序号 求按照学生给出的序号排好历史事件后,所得的事件排序与历史事件实际发生的序列的最长 ...

  5. ruby中=>是什么意思

    如果是对数组赋值,下标 => 值例如 a = {1 => "1",2 => "22"}a[1] "1"a[2] " ...

  6. Linux 初始化系统 systemd - journald 日志

    journalctl 中文手册 archlinux - journal systemd-journald 用于检索 systemd 的日志,是 systemd 自带的日志系统. 1. systemd- ...

  7. Centos安装GD库

    tar zxvf ncurses-5.6.tar.gz 进入目录 cd ncurses-5.6 生成 makefile文件,再进一步编译 ./configure --prefix=/usr --wit ...

  8. poj2377Bad Cowtractors (最小生成树变形之——最大生成树)

    题目链接:http://poj.org/problem?id=2377 Description Bessie has been hired to build a cheap internet netw ...

  9. Linux中ssh及scp的连接

    1. 当你想获取另外一台电脑上的数据时,可以使用这个命令 scp -P 10022 root@172.30.83.173:~/ubuntu1.tar ./ -r   代表传输文件夹,直接传文件可以不加 ...

  10. Jmeter录制web和app脚本

    前置: 一.已经安装了jmeter 步骤: 一.打开jmeter,右键测试计划-->添加添加线程组-->线程-->线程组,新建线程组 二.右键线程组-->添加-->逻辑控 ...