Submission Details

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?

int climbStairs(int n) {
int step[n];
int i = 0;
if(n <= 2)
return n;
step[0] = 1;
step[1] = 2;
for(i = 2; i < n; i++)
{
step[i] = step[i - 2] + step[i - 1];
}
return step[i - 1];
}
  • 用斐波那契的递归处理,到44时就会消耗过大而失败;
  • 递归在程序最后时,都是可以用for来代替的
  • 在for里面定义的变量,对于for外面也是不可见的!
posted @ 2016-06-15 14:36  dylqt  阅读(172)  评论(0编辑  收藏  举报