Skip to content

Commit 657f425

Browse files
committed
feat: add solutions to lc/lcof2 problem: All Paths From Source to Target
lcof2.No.110 & lc.No.0797. All Paths From Source to Target
1 parent 69c2768 commit 657f425

File tree

9 files changed

+463
-38
lines changed

9 files changed

+463
-38
lines changed

lcof2/剑指 Offer II 110. 所有路径/README.md

+99-2
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,124 @@
6666

6767
<p><meta charset="UTF-8" />注意:本题与主站 797&nbsp;题相同:<a href="https://leetcode-cn.com/problems/all-paths-from-source-to-target/">https://leetcode-cn.com/problems/all-paths-from-source-to-target/</a></p>
6868

69-
7069
## 解法
7170

7271
<!-- 这里可写通用的实现逻辑 -->
7372

73+
DFS。
74+
7475
<!-- tabs:start -->
7576

7677
### **Python3**
7778

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

8081
```python
81-
82+
class Solution:
83+
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
84+
ans = []
85+
86+
def dfs(i, path):
87+
if i == len(graph) - 1:
88+
ans.append(path.copy())
89+
return
90+
for j in graph[i]:
91+
path.append(j)
92+
dfs(j, path)
93+
path.pop(-1)
94+
95+
dfs(0, [0])
96+
return ans
8297
```
8398

8499
### **Java**
85100

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

88103
```java
104+
class Solution {
105+
private List<List<Integer>> ans;
106+
private int[][] graph;
107+
108+
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
109+
ans = new ArrayList<>();
110+
this.graph = graph;
111+
List<Integer> path = new ArrayList<>();
112+
path.add(0);
113+
dfs(0, path);
114+
return ans;
115+
}
116+
117+
private void dfs(int i, List<Integer> path) {
118+
if (i == graph.length - 1) {
119+
ans.add(new ArrayList<>(path));
120+
return;
121+
}
122+
for (int j : graph[i]) {
123+
path.add(j);
124+
dfs(j, path);
125+
path.remove(path.size() - 1);
126+
}
127+
}
128+
}
129+
```
130+
131+
### **C++**
132+
133+
```cpp
134+
class Solution {
135+
public:
136+
vector<vector<int>> graph;
137+
vector<vector<int>> ans;
138+
139+
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
140+
this->graph = graph;
141+
vector<int> path;
142+
path.push_back(0);
143+
dfs(0, path);
144+
return ans;
145+
}
146+
147+
void dfs(int i, vector<int> path) {
148+
if (i == graph.size() - 1)
149+
{
150+
ans.push_back(path);
151+
return;
152+
}
153+
for (int j : graph[i])
154+
{
155+
path.push_back(j);
156+
dfs(j, path);
157+
path.pop_back();
158+
}
159+
}
160+
};
161+
```
89162
163+
### **Go**
164+
165+
```go
166+
func allPathsSourceTarget(graph [][]int) [][]int {
167+
var path []int
168+
path = append(path, 0)
169+
var ans [][]int
170+
171+
var dfs func(i int)
172+
dfs = func(i int) {
173+
if i == len(graph)-1 {
174+
ans = append(ans, append([]int(nil), path...))
175+
return
176+
}
177+
for _, j := range graph[i] {
178+
path = append(path, j)
179+
dfs(j)
180+
path = path[:len(path)-1]
181+
}
182+
}
183+
184+
dfs(0)
185+
return ans
186+
}
90187
```
91188

92189
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> graph;
4+
vector<vector<int>> ans;
5+
6+
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
7+
this->graph = graph;
8+
vector<int> path;
9+
path.push_back(0);
10+
dfs(0, path);
11+
return ans;
12+
}
13+
14+
void dfs(int i, vector<int> path) {
15+
if (i == graph.size() - 1)
16+
{
17+
ans.push_back(path);
18+
return;
19+
}
20+
for (int j : graph[i])
21+
{
22+
path.push_back(j);
23+
dfs(j, path);
24+
path.pop_back();
25+
}
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func allPathsSourceTarget(graph [][]int) [][]int {
2+
var path []int
3+
path = append(path, 0)
4+
var ans [][]int
5+
6+
var dfs func(i int)
7+
dfs = func(i int) {
8+
if i == len(graph)-1 {
9+
ans = append(ans, append([]int(nil), path...))
10+
return
11+
}
12+
for _, j := range graph[i] {
13+
path = append(path, j)
14+
dfs(j)
15+
path = path[:len(path)-1]
16+
}
17+
}
18+
19+
dfs(0)
20+
return ans
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
private List<List<Integer>> ans;
3+
private int[][] graph;
4+
5+
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
6+
ans = new ArrayList<>();
7+
this.graph = graph;
8+
List<Integer> path = new ArrayList<>();
9+
path.add(0);
10+
dfs(0, path);
11+
return ans;
12+
}
13+
14+
private void dfs(int i, List<Integer> path) {
15+
if (i == graph.length - 1) {
16+
ans.add(new ArrayList<>(path));
17+
return;
18+
}
19+
for (int j : graph[i]) {
20+
path.add(j);
21+
dfs(j, path);
22+
path.remove(path.size() - 1);
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
3+
ans = []
4+
5+
def dfs(i, path):
6+
if i == len(graph) - 1:
7+
ans.append(path.copy())
8+
return
9+
for j in graph[i]:
10+
path.append(j)
11+
dfs(j, path)
12+
path.pop(-1)
13+
14+
dfs(0, [0])
15+
return ans

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

+116-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
<li>你可以把路径以任意顺序输出,但在路径内的结点的顺序必须保证。</li>
5757
</ul>
5858

59-
6059
## 解法
6160

6261
<!-- 这里可写通用的实现逻辑 -->
@@ -69,6 +68,8 @@
6968

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

71+
BFS:
72+
7273
```python
7374
class Solution:
7475
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
@@ -86,10 +87,32 @@ class Solution:
8687
return ans
8788
```
8889

90+
DFS:
91+
92+
```python
93+
class Solution:
94+
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
95+
ans = []
96+
97+
def dfs(i, path):
98+
if i == len(graph) - 1:
99+
ans.append(path.copy())
100+
return
101+
for j in graph[i]:
102+
path.append(j)
103+
dfs(j, path)
104+
path.pop(-1)
105+
106+
dfs(0, [0])
107+
return ans
108+
```
109+
89110
### **Java**
90111

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

114+
BFS:
115+
93116
```java
94117
class Solution {
95118
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
@@ -115,6 +138,98 @@ class Solution {
115138
}
116139
```
117140

141+
DFS:
142+
143+
```java
144+
class Solution {
145+
private List<List<Integer>> ans;
146+
private int[][] graph;
147+
148+
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
149+
ans = new ArrayList<>();
150+
this.graph = graph;
151+
List<Integer> path = new ArrayList<>();
152+
path.add(0);
153+
dfs(0, path);
154+
return ans;
155+
}
156+
157+
private void dfs(int i, List<Integer> path) {
158+
if (i == graph.length - 1) {
159+
ans.add(new ArrayList<>(path));
160+
return;
161+
}
162+
for (int j : graph[i]) {
163+
path.add(j);
164+
dfs(j, path);
165+
path.remove(path.size() - 1);
166+
}
167+
}
168+
}
169+
```
170+
171+
### **C++**
172+
173+
DFS:
174+
175+
```cpp
176+
class Solution {
177+
public:
178+
vector<vector<int>> graph;
179+
vector<vector<int>> ans;
180+
181+
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
182+
this->graph = graph;
183+
vector<int> path;
184+
path.push_back(0);
185+
dfs(0, path);
186+
return ans;
187+
}
188+
189+
void dfs(int i, vector<int> path) {
190+
if (i == graph.size() - 1)
191+
{
192+
ans.push_back(path);
193+
return;
194+
}
195+
for (int j : graph[i])
196+
{
197+
path.push_back(j);
198+
dfs(j, path);
199+
path.pop_back();
200+
}
201+
}
202+
};
203+
```
204+
205+
DFS:
206+
207+
### **Go**
208+
209+
```go
210+
func allPathsSourceTarget(graph [][]int) [][]int {
211+
var path []int
212+
path = append(path, 0)
213+
var ans [][]int
214+
215+
var dfs func(i int)
216+
dfs = func(i int) {
217+
if i == len(graph)-1 {
218+
ans = append(ans, append([]int(nil), path...))
219+
return
220+
}
221+
for _, j := range graph[i] {
222+
path = append(path, j)
223+
dfs(j)
224+
path = path[:len(path)-1]
225+
}
226+
}
227+
228+
dfs(0)
229+
return ans
230+
}
231+
```
232+
118233
### **...**
119234

120235
```

0 commit comments

Comments
 (0)