File tree 4 files changed +112
-4
lines changed
solution/0700-0799/0797.All Paths From Source to Target
4 files changed +112
-4
lines changed Original file line number Diff line number Diff line change 61
61
62
62
<!-- 这里可写通用的实现逻辑 -->
63
63
64
+ 因为图中不存在环,所以直接用 DFS 或 BFS 遍历即可
65
+
64
66
<!-- tabs:start -->
65
67
66
68
### ** Python3**
67
69
68
70
<!-- 这里可写当前语言的特殊实现逻辑 -->
69
71
70
72
``` python
71
-
73
+ class Solution :
74
+ def allPathsSourceTarget (self , graph : List[List[int ]]) -> List[List[int ]]:
75
+ n = len (graph)
76
+ q = deque([[0 ]])
77
+ ans = []
78
+ while q:
79
+ path = q.popleft()
80
+ u = path[- 1 ]
81
+ if u == n - 1 :
82
+ ans.append(path)
83
+ continue
84
+ for v in graph[u]:
85
+ q.append(path + [v])
86
+ return ans
72
87
```
73
88
74
89
### ** Java**
75
90
76
91
<!-- 这里可写当前语言的特殊实现逻辑 -->
77
92
78
93
``` java
79
-
94
+ class Solution {
95
+ public List<List<Integer > > allPathsSourceTarget (int [][] graph ) {
96
+ int n = graph. length;
97
+ Queue<List<Integer > > queue = new ArrayDeque<> ();
98
+ queue. offer(Arrays . asList(0 ));
99
+ List<List<Integer > > ans = new ArrayList<> ();
100
+ while (! queue. isEmpty()) {
101
+ List<Integer > path = queue. poll();
102
+ int u = path. get(path. size() - 1 );
103
+ if (u == n - 1 ) {
104
+ ans. add(path);
105
+ continue ;
106
+ }
107
+ for (int v : graph[u]) {
108
+ List<Integer > next = new ArrayList<> (path);
109
+ next. add(v);
110
+ queue. offer(next);
111
+ }
112
+ }
113
+ return ans;
114
+ }
115
+ }
80
116
```
81
117
82
118
### ** ...**
Original file line number Diff line number Diff line change 59
59
60
60
## Solutions
61
61
62
+ Since there is no ring in the graph, you can simply use DFS or BFS to traverse it
63
+
62
64
<!-- tabs:start -->
63
65
64
66
### ** Python3**
65
67
66
68
``` python
67
-
69
+ class Solution :
70
+ def allPathsSourceTarget (self , graph : List[List[int ]]) -> List[List[int ]]:
71
+ n = len (graph)
72
+ q = deque([[0 ]])
73
+ ans = []
74
+ while q:
75
+ path = q.popleft()
76
+ u = path[- 1 ]
77
+ if u == n - 1 :
78
+ ans.append(path)
79
+ continue
80
+ for v in graph[u]:
81
+ q.append(path + [v])
82
+ return ans
68
83
```
69
84
70
85
### ** Java**
71
86
72
87
``` java
73
-
88
+ class Solution {
89
+ public List<List<Integer > > allPathsSourceTarget (int [][] graph ) {
90
+ int n = graph. length;
91
+ Queue<List<Integer > > queue = new ArrayDeque<> ();
92
+ queue. offer(Arrays . asList(0 ));
93
+ List<List<Integer > > ans = new ArrayList<> ();
94
+ while (! queue. isEmpty()) {
95
+ List<Integer > path = queue. poll();
96
+ int u = path. get(path. size() - 1 );
97
+ if (u == n - 1 ) {
98
+ ans. add(path);
99
+ continue ;
100
+ }
101
+ for (int v : graph[u]) {
102
+ List<Integer > next = new ArrayList<> (path);
103
+ next. add(v);
104
+ queue. offer(next);
105
+ }
106
+ }
107
+ return ans;
108
+ }
109
+ }
74
110
```
75
111
76
112
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <List <Integer >> allPathsSourceTarget (int [][] graph ) {
3
+ int n = graph .length ;
4
+ Queue <List <Integer >> queue = new ArrayDeque <>();
5
+ queue .offer (Arrays .asList (0 ));
6
+ List <List <Integer >> ans = new ArrayList <>();
7
+ while (!queue .isEmpty ()) {
8
+ List <Integer > path = queue .poll ();
9
+ int u = path .get (path .size () - 1 );
10
+ if (u == n - 1 ) {
11
+ ans .add (path );
12
+ continue ;
13
+ }
14
+ for (int v : graph [u ]) {
15
+ List <Integer > next = new ArrayList <>(path );
16
+ next .add (v );
17
+ queue .offer (next );
18
+ }
19
+ }
20
+ return ans ;
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def allPathsSourceTarget (self , graph : List [List [int ]]) -> List [List [int ]]:
3
+ n = len (graph )
4
+ q = deque ([[0 ]])
5
+ ans = []
6
+ while q :
7
+ path = q .popleft ()
8
+ u = path [- 1 ]
9
+ if u == n - 1 :
10
+ ans .append (path )
11
+ continue
12
+ for v in graph [u ]:
13
+ q .append (path + [v ])
14
+ return ans
You can’t perform that action at this time.
0 commit comments