797:所有可能的路径(修改版)
This commit is contained in:
parent
c0b77f3d14
commit
b031bc8980
@ -62,8 +62,9 @@
|
|||||||
|
|
||||||
package leetcode.editor.cn;
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import javafx.util.Pair;
|
||||||
import java.util.List;
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
//797:所有可能的路径
|
//797:所有可能的路径
|
||||||
class AllPathsFromSourceToTarget {
|
class AllPathsFromSourceToTarget {
|
||||||
@ -75,46 +76,29 @@ class AllPathsFromSourceToTarget {
|
|||||||
//力扣代码
|
//力扣代码
|
||||||
//leetcode submit region begin(Prohibit modification and deletion)
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
class Solution {
|
class Solution {
|
||||||
// List<List<Integer>> result = new ArrayList<>();
|
|
||||||
//
|
|
||||||
// public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
|
|
||||||
// path(graph, 0, new ArrayList<>());
|
|
||||||
// return result;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// public void path(int[][] graph, int index, List<Integer> list) {
|
|
||||||
// list.add(index);
|
|
||||||
// if (index == graph.length) {
|
|
||||||
// result.add(new ArrayList<>(list));
|
|
||||||
// list.remove(list.size() - 1);
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// for (int i = 0; i < graph[index].length; i++) {
|
|
||||||
// path(graph, graph[index][i], list);
|
|
||||||
// }
|
|
||||||
// list.remove(list.size() - 1);
|
|
||||||
// }
|
|
||||||
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
|
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
|
||||||
return solve(graph, 0);
|
List<List<Integer>> result = new ArrayList<>();
|
||||||
|
Queue<Pair<Integer, List<Integer>>> queue = new LinkedList<>();
|
||||||
|
queue.add(new Pair<>(0, new ArrayList<>(Collections.singletonList(0))));
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
Pair<Integer, List<Integer>> pair = queue.poll();
|
||||||
|
int index = pair.getKey();
|
||||||
|
List<Integer> paths = pair.getValue();
|
||||||
|
for (int gr : graph[index]) {
|
||||||
|
if (paths.contains(gr)) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
paths.add(gr);
|
||||||
public List<List<Integer>> solve(int[][] graph, int node) {
|
if (gr == graph.length - 1) {
|
||||||
int N = graph.length;
|
result.add(new ArrayList<>(paths));
|
||||||
List<List<Integer>> ans = new ArrayList();
|
paths.remove((Integer) gr);
|
||||||
if (node == N - 1) {
|
continue;
|
||||||
List<Integer> path = new ArrayList();
|
|
||||||
path.add(N - 1);
|
|
||||||
ans.add(path);
|
|
||||||
return ans;
|
|
||||||
}
|
}
|
||||||
|
queue.add(new Pair<>(gr, new ArrayList<>(paths)));
|
||||||
for (int nei : graph[node]) {
|
paths.remove((Integer) gr);
|
||||||
for (List<Integer> path : solve(graph, nei)) {
|
|
||||||
path.add(0, node);
|
|
||||||
ans.add(path);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ans;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//leetcode submit region end(Prohibit modification and deletion)
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user