執行報錯:The activity must be exported or contain an intent-filter
阿新 • • 發佈:2021-12-07
給你一個有n個節點的 有向無環圖(DAG),請你找出所有從節點 0到節點 n-1的路徑並輸出(不要求按特定順序)
二維陣列的第 i 個數組中的單元都表示有向圖中 i 號節點所能到達的下一些節點,空就是沒有下一個結點了。
譯者注:有向圖是有方向的,即規定了 a→b 你就不能從 b→a 。
來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/all-paths-from-source-to-target
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。
心之所向,素履以往 生如逆旅,一葦以航import java.util.ArrayList; import java.util.LinkedList; import java.util.List; class Solution { private List<List<Integer>> ret = new ArrayList<>(); private LinkedList<Integer> path = new LinkedList<>(); private int[][] graph; private void solve(int from) { if (from == graph.length - 1) { ret.add(new ArrayList<>(path)); return; } for (int to : graph[from]) { path.offerLast(to); solve(to); path.pollLast(); } } public List<List<Integer>> allPathsSourceTarget(int[][] graph) { this.graph = graph; this.path.offerLast(0); solve(0); return ret; } }