【leetcode】1020. Number of Enclaves
题目如下:
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 <= A.length <= 5001 <= A[i].length <= 5000 <= A[i][j] <= 1- 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的更多相关文章
- 【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】476. Number Complement (java实现)
原题链接 https://leetcode.com/problems/number-complement/ 原题 Given a positive integer, output its comple ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
- 【LeetCode】1128. Number of Equivalent Domino Pairs 等价多米诺骨牌对的数量(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 代码 复杂度分析 日期 题目地址:http ...
- 【LeetCode】447. Number of Boomerangs 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- 【LeetCode】996. Number of Squareful Arrays 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
随机推荐
- [CSP-S模拟测试110]题解
也许是最后一篇了. A.最大或 不错的签到题. 对于二进制位来说,高位的一个1比低位的所有1的贡献总和还要大. 显然,$r$必选,因为$r$中所有1的相对考前.那么考虑如何构造另一个数. 首先$l$和 ...
- videojs实践--兼容ie7,8
1,还是先上图 ie9+,ff,chrome,,,
- if isinstance(obj, int):
http://legacy.python.org/dev/peps/pep-0008/ Object type comparisons should always use isinstance() i ...
- import的项目结构不对
问题如下,在我们新导入一个maven项目时,碰到这样的目录结构,总有点别扭,而且在运行Tomcat的时候,突然发现build i选项下面少了两个我们经常使用的两个选项 window --Perspe ...
- Jenkins+Gitlab+自动化测试配置持续集成
Jenkins安装在win7上 GitLab安装在docker上 需求:本地提交自动化测试代码在gitlab上后,jenkins自动构建,拉下新提交的自动化代码,并且运行 参考的链接: https:/ ...
- Python web自动化测试框架搭建(功能&接口)——unittest介绍
Python UnitTest测试框架介绍 1) TestCase:所有测试用例类继承的基本类, TestCase的实例就是测试用例 2) TestSuite:测试套件 ...
- Aizu - ALDS1_4_C Dictionary
Search III Your task is to write a program of a simple dictionary which implements the following ins ...
- HTML5-Classlist样式操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java包装类,自动装箱,拆箱,以及基本数据类型与字符串的转换
package cn.learn; import java.util.ArrayList; /* 包装类 java.lang中,基本运算类型效率高 装箱:把基本类型数据包装为包装类 1.构造方法 In ...
- BZOJ 1109 (LIS)
题面 传送门 分析 设dp[i]是第i个积木在自己的位置上时,前i个积木中最多能回到自己位置的数目. \(dp[i]=max(dp[j])+1 (i>j,a[i]>a[j],a[i]-a[ ...