Leetcode 1020. Number of Enclaves
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的更多相关文章
- 【leetcode】1020. Number of Enclaves
题目如下: Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land) A move consists ...
- Leetcode之深度优先搜索(DFS)专题-1020. 飞地的数量(Number of Enclaves)
Leetcode之深度优先搜索(DFS)专题-1020. 飞地的数量(Number of Enclaves) 深度优先搜索的解题详细介绍,点击 给出一个二维数组 A,每个单元格为 0(代表海)或 1( ...
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 力不从心 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 ...
- [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 ...
随机推荐
- 前端基础之jQuery(Day55)
阅读目录 一 jQuery是什么? 二 什么是jQuery对象? 三 寻找元素(选择器和筛选器) 四 操作元素(属性,css,文档处理) 扩展方法 (插件机制) 一. jQuery是什么? [1] ...
- Mysql数据表字段设置了默认值,插入数据后默认字段的值却为null,不是默认值
我将mysql的数据表的某个字段设置了默认值为1,当向该表插入数据的时候该字段的值不是默认值,而是null. 我的错误原因: 对数据库的操作我使用了持久化工具mybatis,插入数据的时候插入的是整个 ...
- 对Java的接口和抽象类 的一些了解
学习并转载自: https://mp.weixin.qq.com/s?__biz=MzAxMzQ3NzQ3Nw==&mid=2654251476&idx=4&sn=e66ec4 ...
- koa2.0富文本编辑器的选择历程
本人学习vue和koa2.0,做了一个简单的个人博客,博客自然会需要富文本编辑器的选择,由于nodejs和koa2.0,于是便开始了不断尝试的历程. 一.ueditor 刚开始在百度搜索,自然第一个发 ...
- C# winform 屏蔽鼠标右键 spreadsheet Gear 屏蔽鼠标右键菜单
今天用到spreadsheetGear 插件,然后右键有插件自己的菜单.都是英文的,而且还能打开新的窗体.嵌到程序里面,不太合适,所以着手屏蔽. 刚开始用的Mouse_up,虽然能捕获事件,但是没有K ...
- 开源CMDB详细安装使用
CMDB的GitHub地址: https://github.com/open-cmdb/cmdb 环境说明 [root@WCY ~]# cat /etc/redhat-release CentOS L ...
- Windows 修改个性化时间显示
A goal is a dream with a deadline. Much effort, much prosperity. 我感觉我的时间显示不够人性化.不够个性化 修改注册表 我的系统为Win ...
- Mybatis配置插入数据返回主键ID
需要在insert方法中添加 <insert id="insertSelective" parameterType="com.midou.ott.model.MDA ...
- HttpStatusCode
https://docs.microsoft.com/en-us/dotnet/api/system.net.httpstatuscode?view=netframework-4.7.2 422 Un ...
- thinkphp Composer安装指南
1.首先我们去composer的官网下载composer,当然也可以用命令行的形势下下载,我是在windows安装的.https://www.phpcomposer.com/ 2.下载以后进行安装,一 ...