[Swift]LeetCode70. 爬樓梯 | Climbing Stairs

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-mhxrxftx-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

You are climbing a stair case. It takes n steps to reach to the top.git

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?github

Note: Given n will be a positive integer.數組

Example 1:微信

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:app

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

假設你正在爬樓梯。須要 n 階你才能到達樓頂。spa

每次你能夠爬 1 或 2 個臺階。你有多少種不一樣的方法能夠爬到樓頂呢?code

注意:給定 n 是一個正整數。htm

示例 1:blog

輸入: 2
輸出: 2
解釋: 有兩種方法能夠爬到樓頂。
1.  1 階 + 1 階
2.  2 階

示例 2:

輸入: 3
輸出: 3
解釋: 有三種方法能夠爬到樓頂。
1.  1 階 + 1 階 + 1 階
2.  1 階 + 2 階
3.  2 階 + 1 階

8ms
 1 class Solution {
 2     func climbStairs(_ n: Int) -> Int {
 3         //動態規劃
 4         //把運算的結果保存下來
 5         //下一次運算時直接調用
 6         if n==1 || n==2 {return n}
 7         //建立一個特定大小的數組
 8         //並將其全部值設置爲相同的默認值
 9         var arr:[Int] = Array(repeating: 0 , count: n+1)
10         //斐波那契數列
11         //遞歸方式會時不少運算時重複,致使運算時間超時
12         arr[1] = 1
13         arr[2] = 2
14         for i in 3...n
15         {
16             arr[i] =  arr[i-1] + arr[i-2]
17         }
18         return arr[n] 
19     }
20 }

不須要用數組存下全部的數據:8ms

 1 class Solution {
 2     func climbStairs(_ n: Int) -> Int {
 3         if n < 3 {
 4             return n
 5         }
 6         
 7         var a = 1
 8         var b = 2
 9         var res = 0
10         
11         for _ in 2..<n {
12             res = a + b
13             a = b
14             b = res
15         }
16         
17         return res
18     }
19 }

12ms(與法1只存儲方式不一樣)

 1 class Solution {
 2     func climbStairs(_ n: Int) -> Int 
 3     {
 4     guard n != 1 else { return 1 }
 5 
 6     var stairCount = [Int]()
 7     stairCount.insert(0, at: 0)
 8     stairCount.insert(1, at: 1)
 9     stairCount.insert(2, at: 2)
10     if n > 2 
11         {
12             for i in 3...n 
13             {
14                 stairCount.append(stairCount[i - 1] + stairCount[i - 2])
15             }
16         }
17         return stairCount[n]
18     }
19 }

8ms

 1 class Solution {
 2     func climbStairs(_ n: Int) -> Int {
 3         if n <= 2{
 4             return n
 5         }
 6         var a = 1
 7         var b = 2
 8         var res = 2
 9         for i in 2..<n {
10             res = a + res
11             a = b
12             b = res
13         }
14         return res
15     }
16 }

 8ms

 1 class Solution {
 2   func climbStairs(_ n: Int) -> Int {
 3     var result = 0
 4     var n1 = 3
 5     var n2 = 2
 6     
 7     if n <= 3 {
 8       result = n
 9     } else {
10       for _ in 3..<n {
11         result = n1 + n2
12         n2 = n1
13         n1 = result
14       }
15     }
16     
17     return result
18   }
19 }

12ms

 1 class Solution {
 2     func climbStairs(_ n: Int) -> Int {
 3         if(n < 2){return 1}
 4         var memo = [Int]()
 5         memo.append(1)
 6         memo.append(1)
 7         for i in 2...n{memo.append(memo[i-1] + memo[i-2])}
 8         return memo.last ?? memo[0]
 9     }
10 }

24ms

 1 //  問題分析:動態規劃,f[i]表示第i階的方法數,f[i] = f[i - 1] + 1 + f[i - 2] + 1
 2 
 3 class Solution {
 4     func climbStairs(_ n: Int) -> Int {
 5         var f: [Int] = []
 6         f.append(1)
 7         for i in 1...n {
 8             var one = f[i - 1]
 9             var two = 0
10             if (i - 2 < 0) {
11                 two = 0
12             } else {
13                 two = f[i - 2]
14             }
15             f.append(one + two)
16         }
17         return f[n]
18     }
19 }
相關文章
相關標籤/搜索