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.
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 }
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 }
没有评论:
发表评论