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..62de309849edb 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" @@ -255,6 +255,30 @@ 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: 0, count: n) + f[0] = 1 + var steps = k + + while steps > 0 { + var g = Array(repeating: 0, count: n) + for r in relation { + let a = r[0], b = r[1] + g[b] += f[a] + } + f = g + steps -= 1 + } + + return f[n - 1] + } +} +``` + diff --git "a/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution2.swift" "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution2.swift" new file mode 100644 index 0000000000000..b62995acf5a39 --- /dev/null +++ "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution2.swift" @@ -0,0 +1,19 @@ +class Solution { + func numWays(_ n: Int, _ relation: [[Int]], _ k: Int) -> Int { + var f = Array(repeating: 0, count: n) + f[0] = 1 + var steps = k + + while steps > 0 { + var g = Array(repeating: 0, count: n) + for r in relation { + let a = r[0], b = r[1] + g[b] += f[a] + } + f = g + steps -= 1 + } + + return f[n - 1] + } +} \ No newline at end of file