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. 初识JS 基本语法.基本运算符

    JavaScript概述 JavaScript的历史 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中).后将其改名ScriptEase.( ...

  2. 【转】Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  3. 安装三大浏览器驱动driver

    1.chromedriver 下载地址:https://code.google.com/p/chromedriver/downloads/list 2.Firefox的驱动geckodriver 下载 ...

  4. cdoj1324卿学姐与公主

    地址:http://acm.uestc.edu.cn/#/problem/show/1324 卿学姐与公主 Time Limit: 2000/1000MS (Java/Others)     Memo ...

  5. linux 基础知识总结2

    1. 设定文件text的属性为:文件属主(u) 增加写权限;与文件属主同组用户(g) 增加写权限;其他用户(o) 删除执行权限:chmod ug+w,o-x log2012.log     权限选择参 ...

  6. 一个线程知识点, 一个MongoDB的知识点

    //WINForm窗体中切换前后台线程执行任务: protected void RunOnUI(Action action) { Invoke(action); } protected void Ru ...

  7. Limit CPUID MAX

    Limit CPUID MAX to 3.是指出现在英特尔平台的BIOS设置选项.很多主板也显示成CPUID maximum value limit选项.中文意义是:限制执行CPUID指令返回数值大于 ...

  8. NO.4 Android开发中常用框架及工具

    android-pulltorefresh 一个强大的拉动刷新开源项目,支持各种控件下拉刷新ListView.ViewPager.WevView.ExpandableListView.GridView ...

  9. Dijkstra算法 - 最短路径算法

    2017-07-26 22:30:45 writer:pprp dijkstra算法法则:设置顶点集合S,首先将起始点加入该集合,然后根据起始点到其他顶点的路径长度, 选择路径长度最小的顶点加入到集合 ...

  10. Mybatis层次结构图