Skip to content

Commit 721db1d

Browse files
author
Li Li
committed
add code of 70
1 parent 1465c63 commit 721db1d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Solution {
2+
public int ClimbStairs(int n) {
3+
if (n == 1) return 1;
4+
if (n == 2) return 2;
5+
int[] res = new int[n+1];
6+
res[1] = 1;
7+
res[2] = 2;
8+
for (int i = 3; i <= n; i++) {
9+
res[i] = res[i - 1] + res[i - 2];
10+
}
11+
return res[n];
12+
}
13+
}
14+
15+
// refactor to Fibonacci Number
16+
public class Solution {
17+
public int ClimbStairs(int n) {
18+
if (n == 1) {
19+
return 1;
20+
}
21+
int first = 1;
22+
int second = 2;
23+
for (int i = 3; i <= n; i++) {
24+
int third = first + second;
25+
first = second;
26+
second = third;
27+
}
28+
return second;
29+
}
30+
}

0 commit comments

Comments
 (0)