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. Codeforces Round #302 (Div. 2)

    A. Set of Strings 题意:能否把一个字符串划分为n段,且每段第一个字母都不相同? 思路:判断字符串中出现的字符种数,然后划分即可. #include<iostream> # ...

  2. django联合查询

    假设A表的主键aid作为B表的外键,A表有属性name,那么想查询B表中name为abc的元素就可以这样写: B.objects.all().filter(aid__name = 'abc') __真 ...

  3. 关于JSON字符串

    向客户端返回JSON字符串有两种方法: 1.纯手工拼接: result.append("{"); result.append("\"timu\":\& ...

  4. win7解压的软件开机自启动

    win7让你一个可执行程序开机启动. 运行-->regedit-->HKEY_LOCAL_MACHINE-->SOFTWARE-->Microsoft-->Windows ...

  5. maven私服客户端配置

    <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...

  6. JZ2440裸机点亮LED【学习笔记】

    平台:jz2440 作者:庄泽彬(欢迎转载,请注明作者) 说明:韦东山一期视频学习笔记 一.我们首先来做第一个实验,用汇编语言点亮板子上的LED. 1.1 LED的原理图 从下面的原理图可知LED1是 ...

  7. 【bzoj1115】[POI2009]石子游戏Kam(博弈论)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1115 观察问题,我们能发现前后相邻两堆石子的数量差一定非负,而我们在第i堆石子中移走k ...

  8. sql报字段过大的错误解决方法

    set global max_allowed_packet = 2*1024*1024*10

  9. 基于主从复制的Mysql双机热备+amoeba实现读写分离、均衡负载

    读写分离指的是客户只能在主服务器上写,只能在从服务器上读,当然了,这也是要看配置,你可以在主服务器配置读的功能,但是在从服务器上只能读不能写,因为从服务器是基于binlog对主服务器的复制,如果在从服 ...

  10. Activity启动的四种方式

    Activity启动方式有四种,分别是: standardsingleTopsingleTasksingleInstance 可以根据实际的需求为Activity设置对应的启动模式,从而可以避免创建大 ...