Description
https://leetcode.com/problems/climbing-stairs/
爬楼梯,每次只能爬1步或2步,问到台阶n有多少种爬法
Solving Ideas
斐波那契
时间复杂度:
O
(
n
)
O(n)
O(n)
空间复杂度:
O
(
1
)
O(1)
O(1)
Solution
class Solution {
public int climbStairs(int n) {
if (n == 1 || n == 2) return n;
int a1 = 1, a2 = 2, tmp;
for (int i = 3; i <= n; i++) {
tmp = a2;
a2 += a1;
a1 = tmp;
}
return a2;
}
}