【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 <= 500
1 <= A[i].length <= 500
0 <= 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 ...
随机推荐
- NOIp 基础数论知识点总结
推荐阅读 NOIp 数学知识点总结: https://www.cnblogs.com/greyqz/p/maths.html Basic 常用素数表:https://www.cnblogs.com/g ...
- 12 October
次小生成树 http://poj.org/problem?id=1679 不难得出,次小生成树可以由最小生成树更换一条边得到. 首先构造原图的最小生成树,然后枚举每一条不在最小生成树中的边 (u, v ...
- ReactNative的学习笔记
一.安装nodejs 查看是否安装:npm -v 二.安装react-native命令工具 npm install -g react-native-cli 三.查看 react-native --he ...
- HTTP和HTTPS协议,详解
大纲 一.前言: 先来观察这两张图,第一张访问域名http://www.12306.cn,谷歌浏览器提示不安全链接,第二张是https://kyfw.12306.cn/otn/regist/init, ...
- Delphi中Tlist实例
http://blog.163.com/jiandande3218@126/blog/static/74728469201132721428194/ Delphi中Tlist实例 2011-04-27 ...
- vue.jsc初体验
Vue 1.安装脚手架 (1)npm install -g vue-cli (2)Vue -v //查看是否安装成功 (3)Vue init webpack name(名称) (4)Npm insta ...
- Nginx 模块 - ngx_core_module
原文地址 示例配置 指令 accept_mutex accept_mutex_delay daemon debug_connection debug_points env error_log even ...
- 【ABAP系列】SAP ABAP基础-程序优化及响应速度之LOOP
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-程序优化及 ...
- Hadoop(2): Blocks存储管理及读写
1. Replication: 因为每个HDFS被部署在是低成本的商业硬件上(low cost commodity hardware),所以为了有更佳的Fault Tolerance,HDFS将每个B ...
- 4期Web安全基础
介绍了web安全的各种常见漏洞.视频卡顿,建议直接看网易出品的白帽子视频. 类似的教程还有,网易白帽子的教程:参考简书https://www.jianshu.com/p/1b372ca96b87 在看 ...