LN : leetcode 70 Climbing Stairs

lc 70 Climbing Stairs


70 Climbing Stairs

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.

DP Accepted

这是一道特别经典又简单的动态规划题目,先考虑简单的情况,上第一层的方式只有1种,而上第二层的方式有两种。再考虑复杂的情况,上第n层的前一层要么是n-1层,要么是n-2层。其实,该问题很类似斐波那契数列,还是很容易解答的。

class Solution {
public:
int climbStairs(int n) {
vector<int> stairs(n+1, 0);
stairs[1] = 1;
stairs[2] = 2;
for (int i = 3; i < n+1; i++) stairs[i] = stairs[i-1]+stairs[i-2];
return stairs[n];
}
};
上一篇:重写类的Equals以及重写Linq下的Distinct方法


下一篇:Xcode调用旧版本库出现Undefined symbols for architecture x86_64: ld: symbol(s) not found for architecture x86_64