当前位置:网站首页>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.length
2 <= n <= 15
0 <= graph[i][j] < n
graph[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]));
}
}
}
边栏推荐
- 文件 - 02 上传文件:上传临时文件到服务器
- [PSQL] 复杂查询
- [PSQL] SQL基础教程读书笔记(Chapter1-4)
- 2.(1)栈的链式存储、链栈的操作(图解、注释、代码)
- Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition
- 在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
- Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
- Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
- MySQL的触发器
- 电脑开机密码怎么设置?如何给你的电脑加上“安全锁”
猜你喜欢
2022.07.14_每日一题
Difficulty comparison between high concurrency and multithreading (easy to confuse)
【解决】npm ERR A complete log of this run can be found in npm ERR
嵌入式系统驱动初级【2】——内核模块下_参数和依赖
Obtaining server and client information
【网络攻防】常见的网络攻防技术——黑客攻防(通俗易懂版)
360推送-360推送工具-360批量推送工具
Automatic translation software - batch batch automatic translation software recommendation
【Go报错】go go.mod file not found in current directory or any parent directory 错误解决
文件 - 05 下载文件:根据文件Id下载文件
随机推荐
多进程全局变量失效、变量共享问题
【C语言项目合集】这十个入门必备练手项目,让C语言对你来说不再难学!
SQL Server Datetime2数据类型
2022.07.14_每日一题
Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
nohup principle
postgresql源码学习(34)—— 事务日志⑩ - 全页写机制
van-uploader上传图片,使用base64回显无法预览的问题
【编程题】【Scratch三级】2022.03 冬天下雪了
文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
03-SDRAM:写操作(突发)
03-SDRAM: Write operation (burst)
QFileInfo常规方法
科普 | “大姨太”ETH 和 “小姨太”ETC的爱恨情仇
Some derivation formulas for machine learning backpropagation
PCB抄板
Gradle remove dependency demo
安装gstreamer开发依赖库到项目sysroot目录
Jobject 使用
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?