diff --git "a/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/README.md" "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/README.md" index 1703fe246c239..5393a21a182da 100644 --- "a/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/README.md" +++ "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/README.md" @@ -155,6 +155,25 @@ function numWays(n: number, relation: number[][], k: number): number { } ``` +#### Swift + +```swift +class Solution { + func numWays(_ n: Int, _ relation: [[Int]], _ k: Int) -> Int { + var f = Array(repeating: Array(repeating: 0, count: n), count: k + 1) + f[0][0] = 1 + + for i in 1...k { + for r in relation { + let a = r[0], b = r[1] + f[i][b] += f[i - 1][a] + } + } + return f[k][n - 1] + } +} +``` + diff --git "a/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.swift" "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.swift" new file mode 100644 index 0000000000000..c652759ac9adf --- /dev/null +++ "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.swift" @@ -0,0 +1,14 @@ +class Solution { + func numWays(_ n: Int, _ relation: [[Int]], _ k: Int) -> Int { + var f = Array(repeating: Array(repeating: 0, count: n), count: k + 1) + f[0][0] = 1 + + for i in 1...k { + for r in relation { + let a = r[0], b = r[1] + f[i][b] += f[i - 1][a] + } + } + return f[k][n - 1] + } +}