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?
有个梯子有n阶,每次只能爬1阶或者2阶,为爬到梯子顶共有多少种爬法
依次确定跳到每一阶上的爬法数目
class Solution {
public:
int climbStairs(int n) {
if(n==0)return 0;
if(n==1)return 1;
if(n==2)return 2;
vector<int>stairs(n, 0);
stairs[0]=1;
stairs[1]=2;
for(int i=2; i<n; i++){
stairs[i]=stairs[i-1]+stairs[i-2];
}
return stairs[n-1];
}
};LeetCode: Climbing Stairs [070],布布扣,bubuko.com
LeetCode: Climbing Stairs [070]
原文地址:http://blog.csdn.net/harryhuang1990/article/details/27231489