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 ...
随机推荐
- iOS 自定义滑动切换TabbarItem 觉得设计丑也要做出来的UI效果。。。
UI丑却要继续做的感言: 对UI不满意的时候,就会觉得丑爆了,时间长了,却丑习惯了. 论前一阵子Tabbar 多丑,丑得最后不要tabbar了...但是自定义tabbar 和遇到的问题解决的过程可以记 ...
- Mac 一键显示所有隐藏文件 不要那么六好吧
系统应简洁而有效,对一般用户来说这一点尤为重要.不必要让普通用户知道的信息往往会给他们造成困扰,因而,隐藏掉他们便是个不错的选择,既可以保证系统平稳流畅运行,也可以为用户提供友好界面. 对于开发者而言 ...
- python 课堂笔记-购物车
# Author:leon production_list = [ ('iphone',5800), ('mac pro', 9800), ('bike', 800), ('watch', 10600 ...
- hadoop14---centos 安装activemq
创建activemq目录 [root@node1 ~]# mkdir -p /usr/local/activemq 狐火下载activemq,从用户/download目录把文件cp到/usr/loca ...
- 【Flask】Flask Session操作
### session:1. session的基本概念:session和cookie的作用有点类似,都是为了存储用户相关的信息.不同的是,cookie是存储在本地浏览器,session是一个思路.一个 ...
- Python3.x: pyodbc+FreeTDS+UinxODBC连接sybase数据库(Linux系统)
Python3.x: pyodbc+FreeTDS+UinxODBC连接sybase数据库(Linux系统) 一.安装UinxODBC以及依赖包 yum -y install gcc gcc-c++ ...
- SpringBoot AOP控制Redis自动缓存和更新
导入redis的jar包 <!-- redis --> <dependency> <groupId>org.springframework.boot</gro ...
- 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?
import random import string def GenKey(length): chars = string.ascii_letters + string.digits return ...
- 一键配置IP地址脚本
#/bin/bash NETPWD='/etc/sysconfig/network-scripts/' read -p "please enten net num(1,2,3,4) : &q ...
- ZZ__知识点
1. DLL_PROCESS_ATTACH.DLL_PROCESS_DETACH 打印出相关信息 发现,Java Project 项目中,DLL 在 System.loadLibrary(...) 载 ...