标签:
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 problem is actually a Fibonacci problem. The intuition to solve this problem is, the way to N steps stair is the sum of the way to N-1 and N-2 steps stairs.
Code is Simple
public int climbStairs(int n) {
if (n <= 0) return 0;
if (n == 1) return 1;
if (n == 2) return 2;
int s1 = 1;
int s2 = 2;
int result = 0;
for(int i = 2; i < n; i++){
result = s1 + s2;
s1 = s2;
s2 = result;
}
return result;
}
标签:
原文地址:http://www.cnblogs.com/midan/p/4601374.html