File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
src/com/blankj/easy/_0070 Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -21,5 +21,59 @@ public int climbStairs(int n) {
21
21
public static void main (String [] args ) {
22
22
Solution solution = new Solution ();
23
23
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 ;
24
78
}
25
79
}
You can’t perform that action at this time.
0 commit comments