LeetCode: Climbing Stairs

LeetCode: Climbing Stairs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class ClimbingStairs {
public int climbStairs1(int n) {
assert n > 0;
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
return climbStairs1(n - 1) + climbStairs1(n - 2);
}
public int climbStairs2(int n) {
assert n > 0;
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
int x = 1;
int y = 2;
int result = 0;
for (int i = 3; i <= n; i++) {
result = x + y;
x = y;
y = result;
}
return result;
}
}