Skip to content

Commit 6edb8f6

Browse files
authored
feat: add swift implementation to lcp problem: No.07 (#3742)
1 parent b3d1aea commit 6edb8f6

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lcp/LCP 07. 传递信息/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ function numWays(n: number, relation: number[][], k: number): number {
155155
}
156156
```
157157

158+
#### Swift
159+
160+
```swift
161+
class Solution {
162+
func numWays(_ n: Int, _ relation: [[Int]], _ k: Int) -> Int {
163+
var f = Array(repeating: Array(repeating: 0, count: n), count: k + 1)
164+
f[0][0] = 1
165+
166+
for i in 1...k {
167+
for r in relation {
168+
let a = r[0], b = r[1]
169+
f[i][b] += f[i - 1][a]
170+
}
171+
}
172+
return f[k][n - 1]
173+
}
174+
}
175+
```
176+
158177
<!-- tabs:end -->
159178

160179
<!-- solution:end -->
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
func numWays(_ n: Int, _ relation: [[Int]], _ k: Int) -> Int {
3+
var f = Array(repeating: Array(repeating: 0, count: n), count: k + 1)
4+
f[0][0] = 1
5+
6+
for i in 1...k {
7+
for r in relation {
8+
let a = r[0], b = r[1]
9+
f[i][b] += f[i - 1][a]
10+
}
11+
}
12+
return f[k][n - 1]
13+
}
14+
}

0 commit comments

Comments
 (0)