[Swift]LeetCode70. 爬楼梯 | Climbing Stairs
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9697979.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
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:
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 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
注意:给定 n 是一个正整数。
示例 1:
输入: 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 }