LeetCode - 70 - Climbing Stairs
2017-07-15 15:00
375 查看
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.
其实就是一个斐波那契数列题,求斐波那契数列的第N项是多少。
class Solution {
public:
int climbStairs(int n) {
vector<int> ans(n, 0);
ans[0] = 1;
ans[1] = 2;
for (int i = 2; i < n; ++i) {
ans[i] = ans[i-1] + ans[i-2];
}
return ans[n-1];
}
};
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.
其实就是一个斐波那契数列题,求斐波那契数列的第N项是多少。
class Solution {
public:
int climbStairs(int n) {
vector<int> ans(n, 0);
ans[0] = 1;
ans[1] = 2;
for (int i = 2; i < n; ++i) {
ans[i] = ans[i-1] + ans[i-2];
}
return ans[n-1];
}
};
相关文章推荐
- LeetCode70 Climbing Stairs
- leetcode70——Climbing Stairs
- 【leetcode】【70】Climbing Stairs
- 【leetcode c++】70 Climbing Stairs
- 【LeetCode 70】Climbing Stairs(Python)
- LeetCode 70 Climbing Stairs(记忆化搜索)
- leetcode-70-Climbing Stairs
- LeetCode 70 Climbing Stairs(Python详解及实现)
- leetcode70~Climbing Stairs
- LeetCode70 Climbing Stairs 解题报告
- Leetcode 70 Climbing Stairs 递推
- Leetcode-70: Climbing Stairs (DP, 尾递归,Memorization)
- LeetCode 70 Climbing Stairs
- LeetCode 70 Climbing Stairs
- LeetCode 70:Climbing Stairs
- LeetCode 70 Climbing Stairs
- [LeetCode]70 Climbing Stairs
- LeetCode-70 climbing stairs(方法大总结)
- Java [Leetcode 70]Climbing Stairs
- [LeetCode]: 70: Climbing Stairs