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. Django基础(ORM)

    数据库与ORM 数据库的配置 1    django默认支持sqlite,mysql, oracle,postgresql数据库.  <1> sqlite django默认使用sqlite ...

  2. 在w3cschool学完html,css,javascript,jquery以后,还是不会做前端怎么办?

    w3cschool是一个非盈利性的在线技术学习网站,提供按W3C标准编写的基础教程.完整的看完w3cschool上面的手册,可以基本掌握编程语法.基础性的东西通常都会比较零散,因此,在学习一段时间后, ...

  3. C语言预处理命令之条件编译(#ifdef,#else,#endif,#if等)

    转自:http://www.kuqin.com/language/20090806/66164.html 预处理过程扫描源代码,对其进行初步的转换,产生新的源代码提供给编译器.可见预处理过程先于编译器 ...

  4. Entity FrameWork Code First 之Model分离

    之前一直用DB First新建类库进行使用,最近开始研究Code First.Code First也可以将Model新建在类库里面,然后通过数据迁移等操作生成数据库. 现在说下主要步骤: 1.新建类库 ...

  5. CSS3小图标菜单导航

    在线演示 本地下载

  6. Sublime : python环境

    1.安装python.注意区分32位和64位版本,勾选下图红框实现自动将python安装位置添加到环境变量 2.键盘win+r,输入cmd调出命令行,输入python回车,根据结果查看时候安装成功 3 ...

  7. 会话控制Cookie的应用

    Cookie是一种由服务器发送给客户端的片段信息,存储在客户端浏览器的内存或者硬盘上,在客户端对服务器的请求中发回它.PHP透明地支持HTTP Cookie.可以利用他在远程浏览器端存储数据并以此来跟 ...

  8. wyx20162314实验报告1

    北京电子科技学院BESTI实验报告 课程:程序设计与数据结构 班级: 1623 姓名: 王译潇 学号:20162310 指导教师:娄佳鹏老师.王志强老师 实验日期:2017年3月26号 实验密级: 非 ...

  9. C++中int转为char 以及int 转为string和string 转int和字符串的split

    1.对于int 转为char 直接上代码: 正确做法: void toChar(int b) { char u; ]; _itoa( b, buffer, ); //正确解法一 u = buffer[ ...

  10. Python多类继承中,子类默认继承哪个父类的构造函数__init__

    [1]python中如果子类有自己的构造函数,不会自动调用父类的构造函数,如果需要用到父类的构造函数,则需要在子类的构造函数中显式的调用. [2]如果子类没有自己的构造函数,则会直接从父类继承构造函数 ...