dfs或者bfs

  1. class Solution:
  2. def dfs(self, A, rows, cols, i, j):
  3. if not (0 <= i < rows and 0 <= j < cols):
  4. return
  5. elif A[i][j] == 0:
  6. return
  7. else:
  8. A[i][j] = 0
  9. self.dfs(A, rows, cols, i + 1, j)
  10. self.dfs(A, rows, cols, i - 1, j)
  11. self.dfs(A, rows, cols, i, j + 1)
  12. self.dfs(A, rows, cols, i, j - 1)
  13.  
  14. def numEnclaves(self, A: List[List[int]]) -> int:
  15. rows = len(A)
  16. cols = len(A[0])
  17. for j in range(0, cols):
  18. if A[0][j] == 1:
  19. self.dfs(A, rows, cols, 0, j)
  20. if A[rows - 1][j] == 1:
  21. self.dfs(A, rows, cols, rows - 1, j)
  22.  
  23. for i in range(0, rows):
  24. if A[i][0] == 1:
  25. self.dfs(A, rows, cols, i, 0)
  26. if A[i][cols - 1] == 1:
  27. self.dfs(A, rows, cols, i, cols - 1)
  28.  
  29. ans = 0
  30. for row in A:
  31. for a in row:
  32. if a == 1:
  33. ans += 1
  34. return ans

或者

  1. import queue
  2. class Solution:
  3. def bfs(self, A, rows, cols, i, j):
  4. q = queue.Queue()
  5. A[i][j] = 0
  6. q.put((i, j))
  7. while not q.empty():
  8. p = q.get()
  9. if 0 <= p[0] + 1 < rows and A[p[0] + 1][p[1]] == 1:
  10. A[p[0] + 1][p[1]] = 0
  11. q.put((p[0] + 1, p[1]))
  12. if 0 <= p[0] - 1 < rows and A[p[0] - 1][p[1]] == 1:
  13. A[p[0] - 1][p[1]] = 0
  14. q.put((p[0] - 1, p[1]))
  15. if 0 <= p[1] + 1 < cols and A[p[0]][p[1] + 1] == 1:
  16. A[p[0]][p[1] + 1] = 0
  17. q.put((p[0], p[1] + 1))
  18. if 0 <= p[1] - 1 < cols and A[p[0]][p[1] - 1] == 1:
  19. A[p[0]][p[1] - 1] = 0
  20. q.put((p[0], p[1] - 1))
  21.  
  22. def numEnclaves(self, A: List[List[int]]) -> int:
  23. rows = len(A)
  24. cols = len(A[0])
  25. for j in range(0, cols):
  26. if A[0][j] == 1:
  27. self.bfs(A, rows, cols, 0, j)
  28. if A[rows - 1][j] == 1:
  29. self.bfs(A, rows, cols, rows - 1, j)
  30.  
  31. for i in range(0, rows):
  32. if A[i][0] == 1:
  33. self.bfs(A, rows, cols, i, 0)
  34. if A[i][cols - 1] == 1:
  35. self.bfs(A, rows, cols, i, cols - 1)
  36.  
  37. ans = 0
  38. for row in A:
  39. for a in row:
  40. if a == 1:
  41. ans += 1
  42. 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. iOS 自定义滑动切换TabbarItem 觉得设计丑也要做出来的UI效果。。。

    UI丑却要继续做的感言: 对UI不满意的时候,就会觉得丑爆了,时间长了,却丑习惯了. 论前一阵子Tabbar 多丑,丑得最后不要tabbar了...但是自定义tabbar 和遇到的问题解决的过程可以记 ...

  2. Mac 一键显示所有隐藏文件 不要那么六好吧

    系统应简洁而有效,对一般用户来说这一点尤为重要.不必要让普通用户知道的信息往往会给他们造成困扰,因而,隐藏掉他们便是个不错的选择,既可以保证系统平稳流畅运行,也可以为用户提供友好界面. 对于开发者而言 ...

  3. python 课堂笔记-购物车

    # Author:leon production_list = [ ('iphone',5800), ('mac pro', 9800), ('bike', 800), ('watch', 10600 ...

  4. hadoop14---centos 安装activemq

    创建activemq目录 [root@node1 ~]# mkdir -p /usr/local/activemq 狐火下载activemq,从用户/download目录把文件cp到/usr/loca ...

  5. 【Flask】Flask Session操作

    ### session:1. session的基本概念:session和cookie的作用有点类似,都是为了存储用户相关的信息.不同的是,cookie是存储在本地浏览器,session是一个思路.一个 ...

  6. Python3.x: pyodbc+FreeTDS+UinxODBC连接sybase数据库(Linux系统)

    Python3.x: pyodbc+FreeTDS+UinxODBC连接sybase数据库(Linux系统) 一.安装UinxODBC以及依赖包 yum -y install gcc gcc-c++ ...

  7. SpringBoot AOP控制Redis自动缓存和更新

    导入redis的jar包 <!-- redis --> <dependency> <groupId>org.springframework.boot</gro ...

  8. 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?

    import random import string def GenKey(length): chars = string.ascii_letters + string.digits return ...

  9. 一键配置IP地址脚本

    #/bin/bash NETPWD='/etc/sysconfig/network-scripts/' read -p "please enten net num(1,2,3,4) : &q ...

  10. ZZ__知识点

    1. DLL_PROCESS_ATTACH.DLL_PROCESS_DETACH 打印出相关信息 发现,Java Project 项目中,DLL 在 System.loadLibrary(...) 载 ...