Skip to content

Commit f914837

Browse files
committed
feat: add solutions to lc problem: No.0797.All Paths From Source to Target
1 parent b43d94e commit f914837

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed

solution/0700-0799/0797.All Paths From Source to Target/README.md

+38-2
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,58 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
因为图中不存在环,所以直接用 DFS 或 BFS 遍历即可
65+
6466
<!-- tabs:start -->
6567

6668
### **Python3**
6769

6870
<!-- 这里可写当前语言的特殊实现逻辑 -->
6971

7072
```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
7287
```
7388

7489
### **Java**
7590

7691
<!-- 这里可写当前语言的特殊实现逻辑 -->
7792

7893
```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+
}
80116
```
81117

82118
### **...**

solution/0700-0799/0797.All Paths From Source to Target/README_EN.md

+38-2
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,54 @@
5959

6060
## Solutions
6161

62+
Since there is no ring in the graph, you can simply use DFS or BFS to traverse it
63+
6264
<!-- tabs:start -->
6365

6466
### **Python3**
6567

6668
```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
6883
```
6984

7085
### **Java**
7186

7287
```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+
}
74110
```
75111

76112
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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

0 commit comments

Comments
 (0)