原题链接在这里:https://leetcode.com/problems/all-paths-from-source-lead-to-destination/

题目:

Given the edges of a directed graph, and two nodes source and destination of this graph, determine whether or not all paths starting from source eventually end at destination, that is:

  • At least one path exists from the source node to the destination node
  • If a path exists from the source node to a node with no outgoing edges, then that node is equal to destination.
  • The number of possible paths from source to destination is a finite number.

Return true if and only if all roads from source lead to destination.

Example 1:

Input: n = 3, edges = [[0,1],[0,2]], source = 0, destination = 2
Output: false
Explanation: It is possible to reach and get stuck on both node 1 and node 2.

Example 2:

Input: n = 4, edges = [[0,1],[0,3],[1,2],[2,1]], source = 0, destination = 3
Output: false
Explanation: We have two possibilities: to end at node 3, or to loop over node 1 and node 2 indefinitely.

Example 3:

Input: n = 4, edges = [[0,1],[0,2],[1,3],[2,3]], source = 0, destination = 3
Output: true

Example 4:

Input: n = 3, edges = [[0,1],[1,1],[1,2]], source = 0, destination = 2
Output: false
Explanation: All paths from the source node end at the destination node, but there are an infinite number of paths, such as 0-1-2, 0-1-1-2, 0-1-1-1-2, 0-1-1-1-1-2, and so on.

Example 5:

Input: n = 2, edges = [[0,1],[1,1]], source = 0, destination = 1
Output: false
Explanation: There is infinite self-loop at destination node.

Note:

  1. The given graph may have self loops and parallel edges.
  2. The number of nodes n in the graph is between 1 and 10000
  3. The number of edges in the graph is between 0 and 10000
  4. 0 <= edges.length <= 10000
  5. edges[i].length == 2
  6. 0 <= source <= n - 1
  7. 0 <= destination <= n - 1

题解:

There are 2 cases it should return false.

case 1: it encounters a node that has no outgoing edges, but it is not destination.

case 2: it has cycle.

Otherwise, it returns true.

Could iterate graph with BFS. When indegree of a node becomes negative, then ther is cycle.

Time Complexity: O(n+e). e = edges.length.

Space: O(n+e).

AC Java:

 class Solution {
public boolean leadsToDestination(int n, int[][] edges, int source, int destination) {
Set<Integer> [] graph = new Set[n]; for(int i = 0; i<n; i++){
graph[i] = new HashSet<Integer>();
} int [] inDegrees = new int[n];
for(int [] edge : edges){
graph[edge[0]].add(edge[1]);
inDegrees[edge[1]]++;
} LinkedList<Integer> que = new LinkedList<Integer>();
que.add(source); while(!que.isEmpty()){
int cur = que.poll();
if(graph[cur].size() == 0 && cur != destination){
return false;
} for(int nei : graph[cur]){
if(inDegrees[nei] < 0){
return false;
} inDegrees[nei]--; que.add(nei);
}
} return true;
}
}

Could iterate by DFS too.

If current node has been visited within current DFS, then there is cycle.

When traversing all the nodes, make current node as done.

Time Complexity: O(n+e).

Space: O(n+e).

AC Java:

 class Solution {
public boolean leadsToDestination(int n, int[][] edges, int source, int destination) {
Set<Integer> [] graph = new Set[n];
for(int i = 0; i<n; i++){
graph[i] = new HashSet<Integer>();
} for(int [] edge : edges){
graph[edge[0]].add(edge[1]);
} return dfs(source, destination, graph, new int[n]);
} private boolean dfs(int cur, int destination, Set<Integer> [] graph, int [] visited){
if(visited[cur] != 0){
return visited[cur] == 2;
} if(graph[cur].size() == 0){
return cur == destination;
} visited[cur] = 1;
for(int nei : graph[cur]){
if(!dfs(nei, destination, graph, visited)){
return false;
}
} visited[cur] = 2;
return true;
}
}

LeetCode 1059. All Paths from Source Lead to Destination的更多相关文章

  1. LeetCode 797. All Paths From Source to Target

    题目链接:https://leetcode.com/problems/all-paths-from-source-to-target/description/ Given a directed, ac ...

  2. 【leetcode】All Paths From Source to Target

    题目如下: Given a directed, acyclic graph of N nodes. Find all possible paths from node 0 to node N-1, a ...

  3. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  4. [LeetCode] 62. Unique Paths 唯一路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  5. Data Flow ->> Raw File Source & Raw File Destination

    Raw File Source & Raw File Destination一般用在当有某个package在导入数据或者处理数据需要花费非常长的时间的情况下,可以通过把一些处理好的数据先存到r ...

  6. [LeetCode] All Paths From Source to Target 从起点到目标点到所有路径

    Given a directed, acyclic graph of N nodes.  Find all possible paths from node 0 to node N-1, and re ...

  7. 【LeetCode】797. All Paths From Source to Target 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  8. 75th LeetCode Weekly Contest All Paths From Source to Target

    Given a directed, acyclic graph of N nodes.  Find all possible paths from node 0 to node N-1, and re ...

  9. 【leetcode】797. All Paths From Source to Target

    Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n - 1, find all possible paths fro ...

随机推荐

  1. 74HC573锁存器应用(附英文手册)

    锁存器(LATCH)概念 锁存器(Latch)是一种对脉冲电平敏感的存储单元电路,它们可以在特定输入脉冲电平作用下改变状态. 锁存,就是把信号暂存以维持某种电平状态. 锁存器作用: 缓存 完成高速的控 ...

  2. LOJ2461 完美的队列 分块

    传送门 如果对于每一个操作\(i\)找到这个操作中所有的数都被pop掉的时间\(ed_i\),那么剩下就直接差分覆盖一下就可以了. 那么考虑如何求出\(ed_i\).发现似乎并没有什么数据结构能够维护 ...

  3. (转)Python_如何把Python脚本导出为exe程序

    原文地址:https://www.cnblogs.com/robinunix/p/8426832.html 一.pyinstaller简介 Python是一个脚本语言,被解释器解释执行.它的发布方式: ...

  4. java之spring mvc之ajax

    1.可以使用servletAPI来实现 ajax Controller 类 @Controller public class AjaxController { @RequestMapping(&quo ...

  5. RStudio中安装factoextra包的问题

    最近在做一个R语言的小作业,其中聚类分析部分需要用到factoextra安装包,在RStudio中输入install.packages("factoextra")之后,就一直出现“ ...

  6. 2019 迅雷java面试笔试题 (含面试题解析)

    本人3年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.迅雷等公司offer,岗位是Java后端开发,最终选择去了迅雷. 面试了很多家公司,感觉大部分公司考察的点都差不多 ...

  7. mouseleave和mouseout的区别

    http://www.w3school.com.cn/tiy/t.asp?f=jquery_event_mouseleave_mouseout

  8. java中的参数传递

    Java中只有传值调用(值传递),没有传址调用(址传递或者引用传递).所以在java方法中改变参数的值是不会改变原变量的值的,但为什么改变引用变量的属性值却可以呢?请看下面的解答. java中的数据类 ...

  9. js 获取 对象 属性名称(转载)

    来源:https://www.cnblogs.com/YuyuanNo1/p/9257634.html dataObj = {name : su,age : 26,height : 18cm }; f ...

  10. yum仓库的部署

    https://segmentfault.com/a/1190000013968371 私有yum仓库在企业中的应用还是比较广泛,有方便.快捷.灵活等优势.如某公司安全部门不允许大批量的主机连接互联网 ...