当前位置:网站首页>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();
}
}
}边栏推荐
- Tableviewcell highly adaptive
- 高斯消元求解异或线性方程组
- 面试突击68:为什么 TCP 需要 3 次握手?
- Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
- Cloud native (36) | introduction and installation of harbor in kubernetes
- Leetcode 504. 七进制数
- Application of Gauss elimination
- PHP executes shell script
- Uni app learning summary
- Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
猜你喜欢

SSG framework Gatsby accesses the database and displays it on the page

Wechat applet learning notes 1

protobuf的基本用法

Production of a-modal drag function in antui

Search module use case writing

Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)

挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破

PMM (percona monitoring and management) installation record

Use of tabbarcontroller

Node memory overflow and V8 garbage collection mechanism
随机推荐
Xiaobai makes a wave of deep copy and shallow copy
Flutter Event 派发
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
Wechat applet learning notes 1
Mysql5.7.25 master-slave replication (one-way)
Registration module use case writing
分布式网络通信框架:本地服务怎么发布成RPC服务
JS judge the data types object.prototype.tostring.call and typeof
在同一conda环境下先装Pytroch后装TensorFlow
开发转测试:从0开始的6年自动化之路...
Use of selectors
在.NET 6.0中配置WebHostBuilder
AR model in MATLAB for short-term traffic flow prediction
M-ary number STR to n-ary number
Wechat H5 payment on WAP, for non wechat browsers
Cloud native (36) | introduction and installation of harbor in kubernetes
The combination of officially issued SSL certificate and self signed certificate realizes website two-way authentication
【信息系统项目管理师】初见高项系列精华汇总
Production of a-modal drag function in antui
Tableviewcell highly adaptive