Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lcof2/剑指 Offer II 110. 所有路径/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,35 @@ func allPathsSourceTarget(graph [][]int) [][]int {
}
```

#### Swift

```swift
class Solution {
private var results = [[Int]]()
private var graph = [[Int]]()

func allPathsSourceTarget(_ graph: [[Int]]) -> [[Int]] {
self.graph = graph
var path = [0]
dfs(0, &path)
return results
}

private func dfs(_ node: Int, _ path: inout [Int]) {
if node == graph.count - 1 {
results.append(Array(path))
return
}

for next in graph[node] {
path.append(next)
dfs(next, &path)
path.removeLast()
}
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
24 changes: 24 additions & 0 deletions lcof2/剑指 Offer II 110. 所有路径/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
private var results = [[Int]]()
private var graph = [[Int]]()

func allPathsSourceTarget(_ graph: [[Int]]) -> [[Int]] {
self.graph = graph
var path = [0]
dfs(0, &path)
return results
}

private func dfs(_ node: Int, _ path: inout [Int]) {
if node == graph.count - 1 {
results.append(Array(path))
return
}

for next in graph[node] {
path.append(next)
dfs(next, &path)
path.removeLast()
}
}
}
Loading