非常感谢你阅读本文~
欢迎【点赞】【⭐收藏】【评论】~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~
给定一个有 n
个节点的有向无环图,用二维数组 graph
表示,请找到所有从 0
到 n-1
的路径并输出(不要求按顺序)。
graph
的第 i
个数组中的单元都表示有向图中 i
号节点所能到达的下一些结点(译者注:有向图是有方向的,即规定了 a→b 你就不能从 b→a ),若为空,就是没有下一个节点了。
输入:
graph = [[1,2],[3],[3],[]]
输出:
[[0,1,3],[0,2,3]]
解释:
有两条路径 0 -> 1 -> 3 和 0 -> 2 -> 3
输入:
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]]
输入:
graph = [[1],[]]
输出:
[[0,1]]
输入:
graph = [[1,2,3],[2],[3],[]]
输出:
[[0,1,2,3],[0,2,3],[0,3]]
输入:
graph = [[1,3],[2],[3],[]]
输出:
[[0,1,2,3],[0,3]]
无环图
,要不然就没那么简单了。保证输入为有向无环图 (GAD)
,所以我们可以认为节点间一定有着某种排列的顺序,从头到尾怎样可以有最多的路径呢,那就是在保证没有环路的情况下,所有节点都尽可能多的连接着其他节点。1 << n - 2
。class Solution {
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
List<List<Integer>> ans = new ArrayList<>();
Deque<Integer> stack = new ArrayDeque<>();
stack.offerLast(0);
dfs(graph, stack, ans);
return ans;
}
private void dfs(int[][] graph, Deque<Integer> stack, List<List<Integer>> ans) {
if (stack.peekLast() == graph.length - 1) {
ans.add(new ArrayList<>(stack));
return;
}
for (int to : graph[stack.peekLast()]) {
stack.offerLast(to);
dfs(graph, stack, ans);
stack.pollLast();
}
}
}
void dfs(int **graph, int *graphColSize, int *returnSize, int **returnColumnSizes, int n, int *stack, int stackSize, int **ans) {
int last = stack[stackSize - 1];
if (last == n) {
int *row = malloc(sizeof(int) * stackSize);
memcpy(row, stack, sizeof(int) * stackSize);
ans[*returnSize] = row;
(*returnColumnSizes)[(*returnSize)++] = stackSize;
return;
}
for (int i = 0; i < graphColSize[last]; ++i) {
int to = graph[last][i];
stack[stackSize] = to;
dfs(graph, graphColSize, returnSize, returnColumnSizes, n, stack, stackSize + 1, ans);
}
}
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** allPathsSourceTarget(int** graph, int graphSize, int* graphColSize, int* returnSize, int** returnColumnSizes){
*returnSize = 0;
*returnColumnSizes = malloc(sizeof(int) * (1 << graphSize - 2));
int **ans = malloc(sizeof(int *) * (1 << graphSize - 2));
int *stack = malloc(sizeof(int) * graphSize);
stack[0] = 0;
dfs(graph, graphColSize, returnSize, returnColumnSizes, graphSize - 1, stack, 1, ans);
return ans;
}
class Solution {
private:
void dfs(vector<vector<int>>& graph, vector<int>& stack, vector<vector<int>>& ans) {
if (stack.back() == graph.size() - 1) {
ans.push_back(stack);
return;
}
for (auto& to : graph[stack.back()]) {
stack.push_back(to);
dfs(graph, stack, ans);
stack.pop_back();
}
}
public:
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
vector<vector<int>> ans;
vector<int> stack;
stack.push_back(0);
dfs(graph, stack, ans);
return ans;
}
};
class Solution:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
ans = list()
stack = list()
def dfs():
last = stack[len(stack) - 1]
if last == len(graph) - 1:
ans.append(stack[:])
return
for to in graph[last]:
stack.append(to)
dfs()
stack.pop()
stack.append(0)
dfs()
return ans
func allPathsSourceTarget(graph [][]int) (ans [][]int) {
n := len(graph) - 1
stack := []int{
0}
var dfs func()
dfs = func() {
last := stack[len(stack)-1]
if last == n {
ans = append(ans, append([]int{
}, stack...))
return
}
for _, to := range graph[last] {
stack = append(stack, to)
dfs()
stack = stack[:len(stack)-1]
}
}
dfs()
return
}
impl Solution {
pub fn all_paths_source_target(graph: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let mut ans: Vec<Vec<i32>> = Vec::new();
let mut stack: Vec<i32> = Vec::new();
stack.push(0);
Solution::dfs(&graph, graph.len() as i32 - 1, &mut stack, &mut ans);
ans
}
fn dfs(graph: &Vec<Vec<i32>>, n: i32, stack: &mut Vec<i32>, ans: &mut Vec<Vec<i32>>) {
let last = stack[stack.len() - 1];
if last == n {
ans.push(stack.clone());
return;
}
graph[last as usize].iter().for_each(|to| {
stack.push(to.clone());
Solution::dfs(graph, n, stack, ans);
stack.pop();
});
}
}