当前位置:网站首页>LeetCode 797:所有可能的路径
LeetCode 797:所有可能的路径
2022-06-23 03:54:00 【斯沃福德】
题目:
Java实现:
class Solution {
List<List<Integer>> r=new LinkedList<>();
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
//graph是邻接表,每个元素是一个数组,存有该顶点相连的其他顶点
LinkedList<Integer> path=new LinkedList<>(); //多态,LinkedList才有removerLast()方法!
traverse(graph,0,path);
return r;
}
void traverse(int[][]graph,int s,LinkedList<Integer> path){
path.add(s); // 先添加起始节点
//判断是否迭代到头了,是则完成一个路径
int n=graph.length;
if(s==n-1){
r.add(new LinkedList<>(path));
path.removeLast();//递归完成,移除path中元素,
return;
}
//dfs
for(int k :graph[s]){
traverse(graph,k,path);
}
path.removeLast(); //递归完成,移除path中元素,
}
}
边栏推荐
- Actual combat | multiple intranet penetration through Viper
- Flask Foundation: environment setup + configuration + mapping between URL and attempt + redirection + database connection
- Course design C for freshmen -- clothing management system
- 2022-06-22:golang选择题,以下golang代码输出什么?A:3;B:1;C:4;D:编译失败。 package main import ( “fmt“ ) func mai
- 微信小程序:微信也可以发闪照了闪照制作生成微信小程序源码下载,自定义闪照时间
- Beyond chips and AI, why is hard technology capital becoming more and more "hard core"?
- MySQL import large files (can be millions or hundreds)
- 元数据管理Apache Atlas编译(内嵌式)部署及遇到各种错误记录
- 微信小程序:拼图工具箱
- UI automation positioning edge -xpath actual combat
猜你喜欢

2022-06-22:golang选择题,以下golang代码输出什么?A:3;B:1;C:4;D:编译失败。 package main import ( “fmt“ ) func mai

I have been engaged in software testing for 5 years and have changed jobs for 3 times. I have understood the field of software testing
![[graph theory] - bipartite graph](/img/2d/999820edafe7294440cf1873a26084.png)
[graph theory] - bipartite graph

"Wechat applet - Basics" takes you to understand the routing system of the applet (2)

Emergency response HW review

MVC three-tier architecture

Actual combat | multiple intranet penetration through Viper

HCIP 交换机实验

TabControl style of WPF basic control

Wechat applet example development: run
随机推荐
1183. electricity
微信小程序:凑单满减计算神器
UI自动化定位利器-xpath实战
Arduino temperature and humidity sensor DHT11 (including code)
How to better organize the minimum web api code structure
laravel8实现图片验证码
单行或多行文本溢出,省略号代替
Strong push, quick start to software testing
Direct insertion sort - [common sort method (1/8)]
ICer技能02makefile脚本自跑vcs仿真
HCIP第五次作业
BGP实验
百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划
prometheus、influxdb2.2安装及flume_export下载编译使用
Automatically add watermark to screenshot
centos7部署docker,安装mysql
TabControl style of WPF basic control
轮播图的实现
mongodb分片原理
【C语言】关键字