题目如下:

解题思路:本题大多数人采用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. nginx创建默认虚拟主机

    创建默认虚拟主机配置文件作用:禁止任何人通过ip或未允许的域名访问web服务. 如:vim vhosts/default.conf server { listen 80 default; server ...

  2. 阶段3 1.Mybatis_12.Mybatis注解开发_1 mybatis注解开发的环境搭建

    注解开发是省了IUserDao.xml这个映射文件里面的配置 环境搭建 首先是packaging标签.输入jar 需要准备一个实体类.生成getter和setter还有toString方法 创建dao ...

  3. robotframework json解析

    用robotframework做接口测试,现在用的最多的就是json格式的数据,刚开始接触会感觉一脸懵逼,不知道怎么去取里面的值.在这里简单介绍一下,其实本身json取值不会太难,只要理解层次关系,一 ...

  4. 11 ORA-8102:Index Corruption解析

    11 ORA-8102:Index Corruption解析 [oracle@DSI ~]$ oerr ora 810208102, 00000, "index key not found, ...

  5. awk结合数组统计

    1.统计用户登录类型 #!/bin/bashdeclare -A  shells (定义关联数组shells)while read ll   (读取/etc/passwd,ll为变量) dotype= ...

  6. IP子网的划分

    一.划分子网的具体步骤 已知192.168.1.0/24,划分8个子网(一个ip划分8个子网,即网络位不再是24位)1100 0000.1010 1000.0000 0001.0000 00001 . ...

  7. python+selenium切换窗口(获取句柄信息)

    current_window_handle:获得当前窗口句柄: window_handles:返回所有窗口的句柄到当前会话: switch_to.window(suoyou[0]) ========= ...

  8. java文件编译后,出现xx$1.class的原因

    java编译后的文件名字带有$接数字的就是匿名内部类的编译结果,接名字的就是内部类的编译结果 例如:TestFrame$1.class是匿名内部类的编译结果, TestFrame$MyJob.clas ...

  9. c++ k^1

    如果k是偶数,则使k=k+1:若k是奇数,则使k=k-1.

  10. C语言第十一周作业

        这个作业属于哪个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-scienceclass3-2018/ ...