Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d7a2aae

Browse files
committed
Merge pull request #390 from 0xff-dev/797
Add solution and test-cases for problem 797
2 parents 5c20595 + 12c0c86 commit d7a2aae

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

‎leetcode/701-800/0797.All-Paths-From-Source-to-Target/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
# [797.All Paths From Source to Target][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given a directed acyclic graph (**DAG**) of `n` nodes labeled from 0 to `n - 1`, find all possible paths from node `0` to node `n - 1` and return them in **any order**.
5+
6+
The graph is given as follows: `graph[i]` is a list of all nodes you can visit from node `i` (i.e., there is a directed edge from node i to node `graph[i][j]`).
7+
8+
**Example 1:**
79

8-
**Example 1:**
10+
![example1](./all_1.jpg)
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: graph = [[1,2],[3],[3],[]]
14+
Output: [[0,1,3],[0,2,3]]
15+
Explanation: There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.
1316
```
1417

15-
## 题意
16-
> ...
18+
**Example 2:**
1719

18-
## 题解
20+
![example2](./all_2.jpg)
1921

20-
### 思路1
21-
> ...
22-
All Paths From Source to Target
23-
```go
2422
```
25-
23+
Input: graph = [[4,3,1],[3,2,4],[3],[4],[]]
24+
Output: [[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
25+
```
2626

2727
## 结语
2828

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(graph [][]int) [][]int {
4+
ans := make([][]int, 0)
5+
var dfs func(int, []int)
6+
dfs = func(node int, path []int) {
7+
if node == len(graph)-1 {
8+
dst := make([]int, len(path))
9+
copy(dst, path)
10+
ans = append(ans, dst)
11+
return
12+
}
13+
14+
for _, relNode := range graph[node] {
15+
dfs(relNode, append(path, relNode))
16+
}
17+
}
18+
dfs(0, []int{0})
19+
return ans
520
}

‎leetcode/701-800/0797.All-Paths-From-Source-to-Target/Solution_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect [][]int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{
17+
{1, 2}, {3}, {3}, {},
18+
}, [][]int{
19+
{0, 1, 3}, {0, 2, 3},
20+
}},
21+
{"TestCase2", [][]int{
22+
{4, 3, 1}, {3, 2, 4}, {3}, {4}, {},
23+
}, [][]int{
24+
{0, 4}, {0, 3, 4}, {0, 1, 3, 4}, {0, 1, 2, 3, 4}, {0, 1, 4},
25+
}},
1926
}
2027

2128
// 开始测试
@@ -30,10 +37,10 @@ func TestSolution(t *testing.T) {
3037
}
3138
}
3239

33-
//压力测试
40+
//压力测试
3441
func BenchmarkSolution(b *testing.B) {
3542
}
3643

37-
//使用案列
44+
//使用案列
3845
func ExampleSolution() {
3946
}
10.2 KB
Loading[フレーム]
19.2 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /