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 ...
随机推荐
- 基于layUI调用后台数据实现区域信息级联查询
基于layUI调用后台数据实现区域信息级联查询 1.基本思路 后台提供根据区域编码查询区域列表公共接口 页面初始化调用后台接口加载所有省份 点击省份将省份区域编码传入后台查询该省份下所有地市信息,以此 ...
- 通过fsockopen()方法从中国福彩网获取双色球历史中奖数据
# 以下代码基于 CI 框架 # public function history_draw($page = 0) { set_time_limit(0); $page++; $up2now = dat ...
- day12——生成器、推导式、简单内置函数
day12 生成器 迭代器:python中内置的一种节省空间的工具 生成器的本质就是一个迭代器 迭代器和生成器的区别:一个是pyhton自带的,一个是程序员自己写的 写一个生成器 基于函数 在函数中将 ...
- DRF框架(七) ——三大认证组件之频率组件、jwt认证
drf频率组件源码 1.APIView的dispatch方法的 self.initial(request,*args,**kwargs) 点进去 2.self.check_throttles(re ...
- 唯一ID生成器--雪花算法
在微服务架构,分布式系统中的操作会有一些全局性ID的需求,所以我们不能用数据库本身的自增功能来产生主键值,只能由程序来生成唯一的主键值.我们采用的是twitter的snokeflake(雪花)算法. ...
- 忘记token怎么加入k8s集群
一.概述 新版本的k8s,初始化生成的token,只有24小时.超过时间,就得需要重新生成token,为了避免这种情况,直接生成永久的token 二.操作步骤 1.生成一条永久有效的token kub ...
- C#——零散学习
C#——零散学习0 //控制台输入字符串,转化为int,double,float等数值类型: //Convert.ToXXX32();函数. Convert.ToInt32(); //把字符串转换为i ...
- $(...).wordExport is not a function
参考网址:https://laod.cn/code-audit/jquery-is-not-a-function.html 问题描述: 1.view页面引用的是jquery-1.10.2.min.j ...
- 2019 字节跳动java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.字节跳动等公司offer,岗位是Java后端开发,因为发展原因最终选择去了字节跳动,入职一年时间了,也成为了面 ...
- 带有图标的MessageBox
MessageBoxUtils类的代码如下: Ext.define('org.pine.util.MessageBoxUtils', { singleton: true, /** 普通信息提示框 */ ...