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 *r=new int[n];
r[0]=1;
r[1]=2;
......
r[k]=r[k-1]+r[k-2];
class Solution
{
public:
int climbStairs(int n)
{
if(n<=0)
return 0;
if(n==1)
return 1;
if(n==2)
return 2;
int* r=new int[n];
r[0]=1;
r[1]=2;
for(int i=2;i<n;i++)
{
r[n]=r[n-1]+r[n-2];
}
int res;
res=r[n-1];
delete []r;
return res;
}
};