搜索此博客

2012年10月19日星期五

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?
This is an implementation of Fibonacci numbers.
The recursion method is easy. The iterative one is challenging.
01 public int climbStairs(int n) {
02     if(n==0 || n==1)
03         return 1;
04     int x=1;
05     int y=1;
06     while(--n>0)
07     {
08         int temp=x+y;
09         x=y;
10         y=temp;
11     }      
12     return y;
13 }

没有评论:

发表评论