题目: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?
求解:得到递推公式 :f(n)=f(n-1)+f(n-2)
可以利用数组而非递归求解f(n) ,时间复杂度由O(n*n)将为O(n)
int f(int n){
vector<int> v(n+1,0); //调用vector的构造函数
v[1]=1;
v[2]=2;
for(int i=3;i<n+1;i++ )
v[i]=v[i-1]+v[i-2];
return v[n];
}