Skip to content

Commit 7f036c5

Browse files
author
tanfanhua
committed
f
1 parent 3e61850 commit 7f036c5

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/com/blankj/easy/_0070/Solution.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,59 @@ public int climbStairs(int n) {
2121
public static void main(String[] args) {
2222
Solution solution = new Solution();
2323
System.out.println(solution.climbStairs(3));
24+
System.out.println(solution.myClimbStairs(3));
25+
System.out.println(solution.myClimbStairs2(3));
26+
System.out.println(solution.myClimbStairs3(3));
27+
System.out.println(solution.climbStairs(4));
28+
System.out.println(solution.myClimbStairs(4));
29+
System.out.println(solution.myClimbStairs2(4));
30+
System.out.println(solution.myClimbStairs3(4));
31+
}
32+
33+
public int myClimbStairs(int n) {
34+
if (n <= 0) {
35+
return 0;
36+
}
37+
if (n == 1) {
38+
return 1;
39+
}
40+
if (n == 2) {
41+
return 2;
42+
}
43+
return myClimbStairs(n - 1) + myClimbStairs(n - 2);
44+
}
45+
46+
public int myClimbStairs2(int n) {
47+
if (n <= 0) {
48+
return 0;
49+
}
50+
if (n == 1) {
51+
return 1;
52+
}
53+
if (n == 2) {
54+
return 2;
55+
}
56+
int[] f = new int[n];
57+
f[0] = 1;
58+
f[1] = 2;
59+
for (int i = 2; i < n; i++) {
60+
f[i] = f[i - 1] + f[i - 2];
61+
}
62+
63+
return f[n - 1];
64+
}
65+
66+
public int myClimbStairs3(int n) {
67+
if (n <= 0) {
68+
return 0;
69+
}
70+
int f0 = 1;
71+
int f1 = 1;
72+
while (--n > 0) {
73+
int tmp = f1;
74+
f1 = f1 + f0;
75+
f0 = tmp;
76+
}
77+
return f1;
2478
}
2579
}

0 commit comments

Comments
 (0)