当前位置:网站首页>Force deduction DFS
Force deduction DFS
2022-07-26 09:55:00 【Xiao Tang Xuejie】
Here you are n Of nodes Directed acyclic graph (DAG), Please find all slave nodes 0 To the node n-1 And output ( No specific order is required )
graph[i] Is a slave node i List of all nodes that can be accessed ( That is, the slave node i To the node graph[i][j] There is a directed side ).
Example 1:
Input :graph = [[1,2],[3],[3],[]]
Output :[[0,1,3],[0,2,3]]
explain : There are two paths 0 -> 1 -> 3 and 0 -> 2 -> 3
Example 2:
Input :graph = [[4,3,1],[3,2,4],[3],[4],[]]
Output :[[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
Tips :
n == graph.length
2 <= n <= 15
0 <= graph[i][j] < n
graph[i][j] != i( That is, there is no self ring )
graph[i] All elements in Different from each other
Ensure that the input is Directed acyclic graph (DAG)
source : Power button (LeetCode)
link :https://leetcode.cn/problems/all-paths-from-source-to-target
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
class Solution {
// Create a two-dimensional list
List<List<Integer>> ret = new LinkedList<>();
// Create a stack , use Deque Class to achieve
Deque<Integer> stack = new ArrayDeque<>();
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
stack.offerLast(0); // You have to use offerLast Oh , Otherwise, the result will be the opposite , You can try
dfs(graph, 0, graph.length-1);
return ret;
}
//dfs Depth first search traversal
public void dfs(int[][] graph, int start, int end){
if(start==end){
ret.add(new ArrayList<Integer>(stack));//stack What is it? , I will use it. stack To construct a ArrayList() object , Put every element in ArrayList Inside
return;
}
for(int x : graph[start]){
stack.offerLast(x);
dfs(graph, x, end);
stack.pollLast();
}
}
}
边栏推荐
- 2019 ICPC Asia Yinchuan Regional(水题题解)
- WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
- 图解用户登录验证流程,写得太好了!
- Redis sentinel mode setup under Windows
- Installation and use of cocoapods
- Keeping alive to realize MySQL automatic failover
- 【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
- Login module use case writing
- JS table auto cycle scrolling, mouse move in pause
- Phpexcel export Emoji symbol error
猜你喜欢
regular expression
Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
2022年中科磐云——服务器内部信息获取 解析flag
Fiddler packet capturing tool for mobile packet capturing
服务器内存故障预测居然可以这样做!
[datawhale] [machine learning] Diabetes genetic risk detection challenge
Node 内存溢出及V8垃圾回收机制
面试突击68:为什么 TCP 需要 3 次握手?
Development to testing: a six-year road to automation starting from 0
Principle analysis and source code interpretation of service discovery
随机推荐
MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
[fluorescent character effect]
QT handy notes (III) use qtcharts to draw a line chart in VS
Application of Gauss elimination
Node memory overflow and V8 garbage collection mechanism
JS continuous assignment operation
编写一个在bash / shell 和 PowerShell中均可运行的脚本
Wechat H5 payment on WAP, for non wechat browsers
I finished watching this video on my knees at station B
面试突击68:为什么 TCP 需要 3 次握手?
Leetcode 504. Hex number
Logical architecture of MySQL
Set view dynamic picture
莫队学习总结(二)
Fiddler packet capturing tool for mobile packet capturing
Solve the problem of storing cookies in IE7 & IE8
Vectortilelayer replacement style
Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
Gauss elimination solves the inverse of matrix (Gauss)
服务发现原理分析与源码解读