当前位置:网站首页>2022.07.14_Daily Question
2022.07.14_Daily Question
2022-07-31 07:39:00 【No. い】
797. 所有可能的路径
题目描述
给你一个有 n 个节点的 有向无环图(DAG),请你找出所有从节点 0 到节点 n-1 的路径并输出(不要求按特定顺序)
graph[i] 是一个从节点 i 可以访问的所有节点的列表(即从节点 i 到节点 graph[i][j]存在一条有向边).
示例 1:

输入:graph = [[1,2],[3],[3],[]]
输出:[[0,1,3],[0,2,3]]
解释:有两条路径 0 -> 1 -> 3 和 0 -> 2 -> 3
示例 2:

输入:graph = [[4,3,1],[3,2,4],[3],[4],[]]
输出:[[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
提示:
n == graph.length2 <= n <= 150 <= graph[i][j] < ngraph[i][j] != i(即不存在自环)graph[i]中的所有元素 互不相同- 保证输入为 有向无环图(DAG)
- 深度优先搜索
- 广度优先搜索
- 图
- 回溯
coding
class Solution {
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
List<List<Integer>> res = new ArrayList<>();
int index = 0;
List<Integer> record = new ArrayList<>();
// 从 0 出发
record.add(0);
dfs(graph, res, index, record);
return res;
}
private void dfs(int[][] graph, List<List<Integer>> res, int index, List<Integer> record) {
// 到 n - 1 over
if (index == graph.length - 1) {
// The current record is added to the path of res
res.add(new ArrayList<>(record));
return;
}
for (int i = 0; i < graph[index].length; i++) {
// Add a pointer to the node under
record.add(graph[index][i]);
// Add the current node as the index of the next node location, Starting from the next node to continue dfs
dfs(graph, res, graph[index][i], record);
// 回溯
// 【PS】: list.remove(int 索引)
// list.remove(new Integer(list 集合中的元素))
record.remove(new Integer(graph[index][i]));
}
}
}
边栏推荐
- 第十六章:构建n(5,7)阶素数幻方
- Chapter 17: go back to find the entrance to the specified traverse, "ma bu" or horse stance just look greedy, no back to search traversal, "ma bu" or horse stance just look recursive search NXM board
- 安装gstreamer开发依赖库到项目sysroot目录
- 2022.07.24_每日一题
- bcos简介及自序
- 2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
- Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
- leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
- Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
- 04-SDRAM: Read Operation (Burst)
猜你喜欢

Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题

Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)

‘vite‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。

【Go语言入门教程】Go语言简介

讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)

postgresql源码学习(33)—— 事务日志⑨ - 从insert记录看日志写入整体流程

2022.07.14_每日一题

【微服务】Nacos集群搭建以及加载文件配置

把 VS Code 当游戏机

SCI写作指南
随机推荐
文件 - 04 下载文件: 根据文件下载链接下载文件
2022.07.12_每日一题
单点登录 思维导图
【TA-霜狼_may-《百人计划》】美术2.3 硬表面基础
【微服务】 微服务学习笔记二:Eureka注册中心的介绍及搭建
2022.07.24_每日一题
简单谈谈Feign
Redux state management
2022.07.20_每日一题
基于交替迭代法的交直流混合系统潮流计算matlab程序iEEE9节点系统算例
2. (1) Chained storage of stack, operation of chain stack (illustration, comment, code)
一文读懂 MongoDB 和 MySQL 的差异
第9章 异常try...except...else...finally
Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
tidyverse笔记——tidyr包
2022.07.15_每日一题
Kubernetes scheduling
Detailed explanation of js prototype
R——避免使用 col=0
基于LSTM的诗词生成