LeetCode 1059. All Paths from Source Lead to Destination
原题链接在这里: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
sourcenode to thedestinationnode - If a path exists from the
sourcenode to a node with no outgoing edges, then that node is equal todestination. - The number of possible paths from
sourcetodestinationis 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:
- The given graph may have self loops and parallel edges.
- The number of nodes
nin the graph is between1and10000 - The number of edges in the graph is between
0and10000 0 <= edges.length <= 10000edges[i].length == 20 <= source <= n - 10 <= 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的更多相关文章
- LeetCode 797. All Paths From Source to Target
题目链接:https://leetcode.com/problems/all-paths-from-source-to-target/description/ Given a directed, ac ...
- 【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 ...
- 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). ...
- [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 ...
- Data Flow ->> Raw File Source & Raw File Destination
Raw File Source & Raw File Destination一般用在当有某个package在导入数据或者处理数据需要花费非常长的时间的情况下,可以通过把一些处理好的数据先存到r ...
- [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 ...
- 【LeetCode】797. All Paths From Source to Target 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 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 ...
- 【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 ...
随机推荐
- DRF框架(二)——解析模块(parsers)、异常模块(exception_handler)、响应模块(Response)、三大序列化组件介绍、Serializer组件(序列化与反序列化使用)
解析模块 为什么要配置解析模块 1)drf给我们提供了多种解析数据包方式的解析类 form-data/urlencoded/json 2)我们可以通过配置来控制前台提交的哪些格式的数据后台在解析,哪些 ...
- go switch 和java C#有不同
1 switch 后的语句可以有简单的赋值语句 2 case :后的语句结束后不需要break;默认自动结束 除非以 fallthrough 语句结束,否则分支会自动终止 没有条件的 switch 有 ...
- 关于AWK的10个经典案例
awk是Linux系统下一个处理文本的编程语言工具,能用简短的程序处理标准输入或文件.数据排序.计算以及生成报表等等,应用非常广泛.基本的命令语法:awk option 'pattern {actio ...
- Java知识回顾 (12) package
本资料来自于runoob,略有修改. 为了更好地组织类,Java 提供了包机制,用于区别类名的命名空间. Java 使用包(package)这种机制是为了防止命名冲突,访问控制,提供搜索和定位类(cl ...
- Python 第三方日志框架loguru使用
解决中文乱码问题 项目地址 github: https://github.com/Delgan/loguru 文档:https://loguru.readthedocs.io/en/stable/in ...
- CSS-锚点笔记
注意点: position属性 定义建议元素布局所用的定位机制 {position:static/absolute/relative/fixed;} static:默认值,没有定位 absolute: ...
- Flink入门 - 窗口函数
/* * ProcessWinFunOnWindow */ final StreamExecutionEnvironment streamExecutionEnvironment = StreamEx ...
- django 自定义身份认证
自定义身份认证: Django 自带的认证系统足够应付大多数情况,但你或许不打算使用现成的认证系统.定制自己的项目的权限系统需要了解哪些一些关键点,即Django中哪些部分是能够扩展或替换的.这个文档 ...
- day 03 作业 预科
目录 作业 1.简述变量的组成 2.简述变量名的命名规范 3.简述注释的作用 4.使用turtle库构造一幅图,贴在markdown文档中 作业 1.简述变量的组成 变量由变量名.赋值符号.变量值所组 ...
- javascript中 typeof和instanceof的区别
<一> js中typeof的用法进行了详细的汇总介绍 (1)返回一个变量的基本类型 回顾基本类型(number,string,boolean,null,undefined,object) ...