diff --git a/lcci/08.01.Three Steps Problem/README.md b/lcci/08.01.Three Steps Problem/README.md index 7de99767cbcf1..a1c8d496e7c03 100644 --- a/lcci/08.01.Three Steps Problem/README.md +++ b/lcci/08.01.Three Steps Problem/README.md @@ -141,6 +141,26 @@ int waysToStep(int n) { } ``` +```swift +class Solution { + func waysToStep(_ n: Int) -> Int { + let mod = Int(1e9) + 7 + var a = 1, b = 2, c = 4 + if n == 1 { return a } + if n == 2 { return b } + if n == 3 { return c } + + for _ in 1.. ### 方法二:矩阵快速幂加速递推 diff --git a/lcci/08.01.Three Steps Problem/README_EN.md b/lcci/08.01.Three Steps Problem/README_EN.md index 8cdbcfb8cb862..4eb629cc21f3d 100644 --- a/lcci/08.01.Three Steps Problem/README_EN.md +++ b/lcci/08.01.Three Steps Problem/README_EN.md @@ -143,6 +143,26 @@ int waysToStep(int n) { } ``` +```swift +class Solution { + func waysToStep(_ n: Int) -> Int { + let mod = Int(1e9) + 7 + var a = 1, b = 2, c = 4 + if n == 1 { return a } + if n == 2 { return b } + if n == 3 { return c } + + for _ in 1.. ### Solution 2: Matrix Quick Power to Accelerate Recursion diff --git a/lcci/08.01.Three Steps Problem/Solution.swift b/lcci/08.01.Three Steps Problem/Solution.swift new file mode 100644 index 0000000000000..b7d24920626c5 --- /dev/null +++ b/lcci/08.01.Three Steps Problem/Solution.swift @@ -0,0 +1,17 @@ +class Solution { + func waysToStep(_ n: Int) -> Int { + let mod = Int(1e9) + 7 + var a = 1, b = 2, c = 4 + if n == 1 { return a } + if n == 2 { return b } + if n == 3 { return c } + + for _ in 1..