题目如下:

解题思路:本题大多数人采用DFS的方法,这里我用的是另一种方法。我的思路是建立一次初始值为空的safe数组,然后遍历graph,找到graph[i]中所有元素都在safe中的元素,把i加入safe。遍历完graph后继续重头遍历,直到某一次遍历后无新元素加入safe后停止。safe即为题目要求的答案。以上面例子距离,首先safe是空,第一次遍历graph后,safe=[5,6];第二次遍历后将2和4加入safe;第三次遍历后无新元素加入,safe最终结果为[5,6,2,4]

代码如下:

class Solution(object):
def eventualSafeNodes(self, graph):
"""
:type graph: List[List[int]]
:rtype: List[int]
"""
safe = []
flag = True
dic = {}
while flag:
flag = False
for i,v in enumerate(graph):
exist = True
for j in v:
if j not in dic:
exist = False
break
if exist == True and i not in dic:
safe.append(i)
dic[i] = 1
flag = True
safe.sort()
return safe

【leetcode】802. Find Eventual Safe States的更多相关文章

  1. 【LeetCode】802. Find Eventual Safe States 解题报告(Python)

    [LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  2. LC 802. Find Eventual Safe States

    In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.  ...

  3. LeetCode 802. Find Eventual Safe States

    原题链接在这里:https://leetcode.com/problems/find-eventual-safe-states/ 题目: In a directed graph, we start a ...

  4. [LeetCode] 802. Find Eventual Safe States 找到最终的安全状态

    In a directed graph, we start at some node and every turn, walk along a directed edge of the graph.  ...

  5. 802. Find Eventual Safe States

    https://leetcode.com/problems/find-eventual-safe-states/description/ class Solution { public: vector ...

  6. 【LeetCode】753. Cracking the Safe 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/cracking ...

  7. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  8. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...

  9. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

随机推荐

  1. 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_4 Mybatis的CRUD-查询一个和模糊查询

    模糊查询 测试的时候需要提供百分号的模糊查询.传入的参数提供百分号 所有包含王字的就都查询出来了.

  2. RabbitMQ安装及其中遇到的问题解决方案

    参考官方文档:https://www.rabbitmq.com/install-debian.html#apt 第一步: # import PackageCloud signing key wget ...

  3. HttpClient设置忽略SSL,实现HTTPS访问, 解决Certificates does not conform to algorithm constraints

    话不多说,直接上代码. 测试API:   https://api.k780.com/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f7 ...

  4. 每次进步一点点——linux expect 使用

    1. 介绍 expect是建立在tcl(参见:Tcl/Tk快速入门 )基础上的一个工具,它可以让一些需要交互的任务自动化地完成.相当于模拟了用户和命令行的交互操作. 一个具体的场景:远程登陆服务器,并 ...

  5. sentos7网卡改名

    一.已经装好系统CentOS7修改网卡为eth0 1. 修改网卡配置文件[root@localhost ~]# mv /etc/sysconfig/network-scripts/ifcfg-ens3 ...

  6. 【Qt开发】【Linux开发】QT设置环境变量QWS_DISPLAY

    QT设置环境变量QWS_DISPLAY 当应用程序./myQtApp -qws启动时,会去检测QWS_DISPLAY这个环境变量, 判断界面最终显示在哪个framebuffer中, 如果是虚拟的fra ...

  7. Springboot2.x集成Redis集群模式

    Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...

  8. layui动态渲染select等组件并初始化赋值失败

    描诉:有一个用户信息form表单,其中有部门单选框,数据库中有一张dept(部门)表,要动态渲染出所有部门,并默认选中用户所在部门 关键代码: html页面 <div class="l ...

  9. Android remote gdb

    On Android phone adb push ~/utils/android-ndk-r12b/prebuilt/android-arm64/gdbserver/gdbserver /data/ ...

  10. mysql复习(1)基本CRUD操作

    一.这段时间在学校,把之前的东西都好好捡起来. 0.下面介绍Mysql的最基本的增删改查操作,很多IT工作者都必须掌握的命令,也是IT面试最常考的知识点.在进行增删改查之前,先建立一个包含数据表use ...