其他分享
首页 > 其他分享> > [LeetCode] 1059. All Paths from Source Lead to Destination

[LeetCode] 1059. 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:

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

从始点到终点的所有路径。

给定有向图的边 edges,以及该图的始点 source 和目标终点 destination,确定从始点 source 出发的所有路径是否最终结束于目标终点 destination,即:

  • 从始点 source 到目标终点 destination 存在至少一条路径
  • 如果存在从始点 source 到没有出边的节点的路径,则该节点就是路径终点。
  • 从始点source到目标终点 destination 可能路径数是有限数字
  • 当从始点 source 出发的所有路径都可以到达目标终点 destination 时返回 true,否则返回 false。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/all-paths-from-source-lead-to-destination
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道有向图的题。题目的意思解释的很清楚,给了起点,终点,所有的边,请你判断是不是所有从起点开始的边都能带你去到终点。

遇到图的题,绝大部分还是用DFS去遍历,得到答案。这个题需要注意的点是

思路不难想,代码的实现需要多练,面试才会写的6。

时间O(V + E)

空间O(n)

Java实现 - hashmap建图

 1 class Solution {
 2     public boolean leadsToDestination(int n, int[][] edges, int source, int destination) {
 3         // build the graph
 4         HashMap<Integer, List<Integer>> graph = new HashMap<>();
 5         for (int[] edge : edges) {
 6             graph.putIfAbsent(edge[0], new ArrayList<>());
 7             graph.get(edge[0]).add(edge[1]);
 8         }
 9         return helper(graph, new HashSet<>(), source, destination);
10     }
11 
12     private boolean helper(Map<Integer, List<Integer>> graph, Set<Integer> visited, int cur, int end) {
13         // base case
14         if (!graph.containsKey(cur)) {
15             return cur == end;
16         }
17         visited.add(cur);
18         for (int neighbor : graph.get(cur)) {
19             if (visited.contains(neighbor) || !helper(graph, visited, neighbor, end)) {
20                 return false;
21             }
22         }
23         visited.remove(cur);
24         return true;
25     }
26 }
View Code

 

Java实现 - list建图

 1 class Solution {
 2     public boolean leadsToDestination(int n, int[][] edges, int source, int destination) {
 3         // corner case
 4         if (edges == null || edges.length == 0) {
 5             return true;
 6         }
 7 
 8         // normal case
 9         List<Integer>[] g = new List[n];
10         int[] colors = new int[n];
11         buildGraph(g, edges);
12         return dfs(g, source, destination, colors);
13     }
14 
15     // s = source, d = destination
16     private boolean dfs(List<Integer>[] g, int s, int d, int[] colors) {
17         // base case
18         if (g[s] == null || g[s].size() == 0) {
19             return s == d;
20         }
21         colors[s] = 1;
22         for (int next : g[s]) {
23             if (colors[next] == 1) {
24                 return false;
25             }
26             if (colors[next] == 0 && !dfs(g, next, d, colors)) {
27                 return false;
28             }
29             colors[s] = 2;
30         }
31         return true;
32     }
33 
34     private void buildGraph(List<Integer>[] g, int[][] edges) {
35         for (int[] e : edges) {
36             int from = e[0];
37             int to = e[1];
38             if (g[from] == null) {
39                 g[from] = new LinkedList<>();
40             }
41             g[from].add(to);
42         }
43     }
44 }
View Code

 

LeetCode 题目总结

标签:node,Paths,Lead,int,1059,destination,source,edges,graph
来源: https://www.cnblogs.com/cnoodle/p/13375378.html