70. Climbing Stairs 爬楼梯问题
Description
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.
类似于斐波纳契数列,递归的话会溢出超时,使用动态规划
int climbStairs(int n)//类似于斐波纳契数列
{
if (n < 4) return n;
int temp , b = 3, c = 5;//b=3代表n=3的情况,c=5代表n=4的情况
for (int i = 5; i <= n; i++)//实现X[i] = X[i-1] + X[i-2] ,递归会超时,还是使用动态规划
{
temp = c;
c = b+c;
b = temp;
}
return c;
}
参考文献
http://blog.youkuaiyun.com/jfkidear/article/details/41989337