4
4
import java .util .Map ;
5
5
6
6
/**
7
- *
8
7
* @author Varun Upadhyay (https://github.com/varunu28)
9
- *
8
+ * @author yanglbme (https://github.com/yanglbme)
10
9
*/
11
10
12
11
public class Fibonacci {
13
12
14
- private static Map <Integer ,Integer > map = new HashMap <Integer , Integer >();
13
+ private static Map <Integer , Integer > map = new HashMap <>();
15
14
16
15
public static void main (String [] args ) throws Exception {
17
16
@@ -26,13 +25,8 @@ public static void main(String[] args) throws Exception {
26
25
* This method finds the nth fibonacci number using memoization technique
27
26
*
28
27
* @param n The input n for which we have to determine the fibonacci number
29
- * Outputs the nth fibonacci number
28
+ * Outputs the nth fibonacci number
30
29
**/
31
-
32
-
33
-
34
-
35
-
36
30
private static int fibMemo (int n ) {
37
31
if (map .containsKey (n )) {
38
32
return map .get (n );
@@ -42,10 +36,9 @@ private static int fibMemo(int n) {
42
36
43
37
if (n <= 2 ) {
44
38
f = 1 ;
45
- }
46
- else {
47
- f = fibMemo (n -1 ) + fibMemo (n -2 );
48
- map .put (n ,f );
39
+ } else {
40
+ f = fibMemo (n - 1 ) + fibMemo (n - 2 );
41
+ map .put (n , f );
49
42
}
50
43
51
44
return f ;
@@ -55,55 +48,50 @@ private static int fibMemo(int n) {
55
48
* This method finds the nth fibonacci number using bottom up
56
49
*
57
50
* @param n The input n for which we have to determine the fibonacci number
58
- * Outputs the nth fibonacci number
51
+ * Outputs the nth fibonacci number
59
52
**/
60
-
61
53
private static int fibBotUp (int n ) {
62
54
63
- Map <Integer ,Integer > fib = new HashMap <Integer , Integer >();
55
+ Map <Integer , Integer > fib = new HashMap <>();
64
56
65
- for (int i = 1 ; i < n + 1 ; i ++) {
66
- int f = 1 ;
67
- if (i <= 2 ) {
57
+ for (int i = 1 ; i < n + 1 ; i ++) {
58
+ int f ;
59
+ if (i <= 2 ) {
68
60
f = 1 ;
69
- }
70
- else {
71
- f = fib .get (i -1 ) + fib .get (i -2 );
61
+ } else {
62
+ f = fib .get (i - 1 ) + fib .get (i - 2 );
72
63
}
73
64
fib .put (i , f );
74
65
}
75
66
76
67
return fib .get (n );
77
68
}
78
-
79
-
80
-
69
+
70
+
81
71
/**
82
72
* This method finds the nth fibonacci number using bottom up
83
73
*
84
- * @author Shoaib Rayeen (https://github.com/shoaibrayeen)
85
74
* @param n The input n for which we have to determine the fibonacci number
86
- * Outputs the nth fibonacci number
87
- *
88
- * This is optimized version of Fibonacci Program. Without using Hashmap and recursion.
89
- * It saves both memory and time.
90
- * Space Complexity will be O(1)
91
- * Time Complexity will be O(n)
92
- *
93
- * Whereas , the above functions will take O(n) Space.
75
+ * Outputs the nth fibonacci number
76
+ * <p>
77
+ * This is optimized version of Fibonacci Program. Without using Hashmap and recursion.
78
+ * It saves both memory and time.
79
+ * Space Complexity will be O(1)
80
+ * Time Complexity will be O(n)
81
+ * <p>
82
+ * Whereas , the above functions will take O(n) Space.
83
+ * @author Shoaib Rayeen (https://github.com/shoaibrayeen)
94
84
**/
95
85
private static int fibOptimized (int n ) {
96
-
97
86
if (n == 0 ) {
98
87
return 0 ;
99
88
}
100
- int prev = 0 , res = 1 , next ;
101
- for ( int i = 2 ; i < n ; i ++) {
102
- next = prev + res ;
103
- prev = res ;
104
- res = next ;
89
+ int prev = 0 , res = 1 , next ;
90
+ for (int i = 2 ; i < n ; i ++) {
91
+ next = prev + res ;
92
+ prev = res ;
93
+ res = next ;
105
94
}
106
95
return res ;
107
96
}
108
- }
109
-
97
+ }
0 commit comments