LeetCode70-爬楼梯

LeetCode70-爬楼梯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public int climbStairs(int n) {
if(n <= 0) return 0;
if(n == 1) return 1;
if(n == 2) return 2;

int one_step_before = 2;
int two_steps_before = 1;
int all_ways = 0;

for(int i=2; i<n; i++){
all_ways = one_step_before + two_steps_before;
two_steps_before = one_step_before;
one_step_before = all_ways;
}
return all_ways;
}