当前位置:网站首页>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();
}
}
}边栏推荐
- [information system project manager] summary of essence of high-level series for the first time
- 图解用户登录验证流程,写得太好了!
- R语言ggpubr包ggsummarystats函数可视化分组箱图(自定义分组颜色)并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr、统计值的色彩和分组图色匹配
- MFC handy notes
- POJ 1012 Joseph
- JS judge the data types object.prototype.tostring.call and typeof
- A new paradigm of distributed deep learning programming: Global tensor
- Wechat H5 payment on WAP, for non wechat browsers
- 在.NET 6.0中配置WebHostBuilder
- Installation and use of cocoapods
猜你喜欢

Installation and use of cocoapods

万字详解“用知识图谱驱动企业业绩增长”

高斯消元

解决npm -v突然失效 无反应

PMM (percona monitoring and management) installation record

Interview shock 68: why does TCP need three handshakes?

Leetcode 504. 七进制数

Server and client dual authentication (2)

Gauss elimination solves the inverse of matrix (Gauss)

spolicy请求案例
随机推荐
Leetcode 504. 七进制数
【荧光字效果】
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
在Blazor 中自定义权限验证
IIS website configuration
面试突击68:为什么 TCP 需要 3 次握手?
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
R language ggplot2 visualization: align the legend title to the middle of the legend box in ggplot2 (default left alignment, align legend title to middle of legend)
IIS error prompt after installing Serv-U: hresult:0x80070020
解决ProxyError: Conda cannot proceed due to an error in your proxy configuration.
Server memory failure prediction can actually do this!
新公链Aptos何以拉满市场期待值?
编写一个在bash / shell 和 PowerShell中均可运行的脚本
Vectortilelayer replacement style
The whole process of server environment configuration
B站这个视频我是跪着看完的
Flutter Event 派发
图解用户登录验证流程,写得太好了!
论文笔记(SESSION-BASED RECOMMENDATIONS WITHRECURRENT NEURAL NETWORKS)
新增市场竞争激烈,中国移动被迫推出限制性超低价5G套餐