dfs或者bfs

class Solution:
def dfs(self, A, rows, cols, i, j):
if not (0 <= i < rows and 0 <= j < cols):
return
elif A[i][j] == 0:
return
else:
A[i][j] = 0
self.dfs(A, rows, cols, i + 1, j)
self.dfs(A, rows, cols, i - 1, j)
self.dfs(A, rows, cols, i, j + 1)
self.dfs(A, rows, cols, i, j - 1) def numEnclaves(self, A: List[List[int]]) -> int:
rows = len(A)
cols = len(A[0])
for j in range(0, cols):
if A[0][j] == 1:
self.dfs(A, rows, cols, 0, j)
if A[rows - 1][j] == 1:
self.dfs(A, rows, cols, rows - 1, j) for i in range(0, rows):
if A[i][0] == 1:
self.dfs(A, rows, cols, i, 0)
if A[i][cols - 1] == 1:
self.dfs(A, rows, cols, i, cols - 1) ans = 0
for row in A:
for a in row:
if a == 1:
ans += 1
return ans

或者

import queue
class Solution:
def bfs(self, A, rows, cols, i, j):
q = queue.Queue()
A[i][j] = 0
q.put((i, j))
while not q.empty():
p = q.get()
if 0 <= p[0] + 1 < rows and A[p[0] + 1][p[1]] == 1:
A[p[0] + 1][p[1]] = 0
q.put((p[0] + 1, p[1]))
if 0 <= p[0] - 1 < rows and A[p[0] - 1][p[1]] == 1:
A[p[0] - 1][p[1]] = 0
q.put((p[0] - 1, p[1]))
if 0 <= p[1] + 1 < cols and A[p[0]][p[1] + 1] == 1:
A[p[0]][p[1] + 1] = 0
q.put((p[0], p[1] + 1))
if 0 <= p[1] - 1 < cols and A[p[0]][p[1] - 1] == 1:
A[p[0]][p[1] - 1] = 0
q.put((p[0], p[1] - 1)) def numEnclaves(self, A: List[List[int]]) -> int:
rows = len(A)
cols = len(A[0])
for j in range(0, cols):
if A[0][j] == 1:
self.bfs(A, rows, cols, 0, j)
if A[rows - 1][j] == 1:
self.bfs(A, rows, cols, rows - 1, j) for i in range(0, rows):
if A[i][0] == 1:
self.bfs(A, rows, cols, i, 0)
if A[i][cols - 1] == 1:
self.bfs(A, rows, cols, i, cols - 1) ans = 0
for row in A:
for a in row:
if a == 1:
ans += 1
return ans

Leetcode 1020. Number of Enclaves的更多相关文章

  1. 【leetcode】1020. Number of Enclaves

    题目如下: Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land) A move consists ...

  2. Leetcode之深度优先搜索(DFS)专题-1020. 飞地的数量(Number of Enclaves)

    Leetcode之深度优先搜索(DFS)专题-1020. 飞地的数量(Number of Enclaves) 深度优先搜索的解题详细介绍,点击 给出一个二维数组 A,每个单元格为 0(代表海)或 1( ...

  3. C#版 - Leetcode 191. Number of 1 Bits-题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  4. [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 surro ...

  5. [leetcode]694. Number of Distinct Islands你究竟有几个异小岛?

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  6. [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  7. [LeetCode] 694. Number of Distinct Islands

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  8. 力不从心 Leetcode(ugly number heap) 263, 264,313

    Leetcode ugly number set (3 now) new ugly number is generated by multiplying a prime with previous g ...

  9. [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 surro ...

随机推荐

  1. samba、nginx服务

    一.部署samba Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成,Samba主要用于Linux或UNIX和Windows系统之间的文件共享. SMB( ...

  2. 详解HTTP中GET与POST的区别,不是你看过的标准答案!

    防吐槽声明:本文适合程序员新人,自认阅文无数.技术超叼的大神不用看. GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含 ...

  3. iOS程序的启动过程介绍

    大家在学习iPhone开发时候,都会写HelloWorld程序.大家一般都是通过向导,生成项目,然后通过模拟器启动应用程序.但是大家知道其背后的启动过程吗?也就是当点击程序图标启动程序开始到退出程序整 ...

  4. python报错 TypeError: string indices must be integers

    所以在读取字典的时候,最好先判断类型,然后再查看它是否已经有这样的属性: type(mydict) == type({})             #检查不是字典 如果是字典,再看看有没有这样的属性: ...

  5. Loadrunner场景设计篇——负载生成器

    1  简介 当执行一个场景时,Controller把场景中的每个用户配到负载生成器(Load generator). 所谓的负载生成器(Load Generator)就是执行Vuser脚本,运行Vus ...

  6. Java伪代码示例

    学习并转载自https://www.cnblogs.com/z245894546/p/7535261.html import.java.大道至简.*; import.java.愚公移山.*; publ ...

  7. powerdesigner解决创建多个表表主键名称重复的问题

    选择菜单栏的tools选项,选择Model  Options..,选择Model Settings  只要将图片中的选择打钩去掉即可(操作步骤1),同时设置为默认选项(操作步骤2),防止以后问题又出现 ...

  8. java springboot整合zookeeper入门教程(增删改查)

    java springboot整合zookeeper增删改查入门教程 zookeeper的安装与集群搭建参考:https://www.cnblogs.com/zwcry/p/10272506.html ...

  9. level-13

    如何调试IE浏览器 1.打开IE浏览器,F12打开开发者模式.(针对IE7及以上) 2.针对IE6浏览器.使用虚拟机或者用ietester 什么是CSS hack?在 CSS 和 HTML里如何写 h ...

  10. Github fork其他项目的分支与主干保持同步

    Fork一个Repo Fork是一个复制的操作,当你Fork一个项目之后,你就有了在原项目的基础之上进行修改和扩展的权限. 通常情况下,Fork操作用于参与别人的项目(成为项目中的一员),或者以别人的 ...