class Solution {
boolean[] visited;
int n;
ArrayList<List<Integer>> res;
ArrayList<Integer> tmp=new ArrayList<>();
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
if(graph==null||graph.length==0)
return new ArrayList<>();
int a=graph.length;
int b=graph[0].length;
n=a;
visited=new boolean[n];
Arrays.fill(visited,false);
res=new ArrayList<List<Integer>>();
dfs(0,graph);
return res;
}
public void dfs(int x,int[][] graph){
if(x>n-1||visited[x]){
return;
}
tmp.add(x);
visited[x]=true;
if(x==n-1){
ArrayList<Integer> ttmp=new ArrayList<>();
ttmp.addAll(tmp);
res.add(ttmp);
}
else{
for(int i=0;i<graph[x].length;i++){
dfs(graph[x][i],graph);
}
}
visited[x]=false;//返回上一级前把当前状态清掉
tmp.remove((Integer)x);
return;
}
}
LeetCode 797
最新推荐文章于 2021-08-27 09:17:54 发布