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?
递归导致超时,所以非递归。
public long climbStairs(long n) {
if (n <= 2) return n;
long f1 = 1;
long f2 = 2;
long f3 = 0;
int i = 3;
while (i++ <= n) {
f3 = f1 + f2;
f1 = f2;
f2 = f3;
}
return f3;
}