Skip to content

Commit bb670a2

Browse files
committed
fix: remove unnecesary assignation to fix TheAlgorithms#698
- Fix TheAlgorithms#698 - Thanks @lprone
1 parent f2f7982 commit bb670a2

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

Dynamic Programming/Fibonacci.java

+30-42
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
import java.util.Map;
55

66
/**
7-
*
87
* @author Varun Upadhyay (https://github.com/varunu28)
9-
*
8+
* @author yanglbme (https://github.com/yanglbme)
109
*/
1110

1211
public class Fibonacci {
1312

14-
private static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
13+
private static Map<Integer, Integer> map = new HashMap<>();
1514

1615
public static void main(String[] args) throws Exception {
1716

@@ -26,13 +25,8 @@ public static void main(String[] args) throws Exception {
2625
* This method finds the nth fibonacci number using memoization technique
2726
*
2827
* @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
3029
**/
31-
32-
33-
34-
35-
3630
private static int fibMemo(int n) {
3731
if (map.containsKey(n)) {
3832
return map.get(n);
@@ -42,10 +36,9 @@ private static int fibMemo(int n) {
4236

4337
if (n <= 2) {
4438
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);
4942
}
5043

5144
return f;
@@ -55,55 +48,50 @@ private static int fibMemo(int n) {
5548
* This method finds the nth fibonacci number using bottom up
5649
*
5750
* @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
5952
**/
60-
6153
private static int fibBotUp(int n) {
6254

63-
Map<Integer,Integer> fib = new HashMap<Integer,Integer>();
55+
Map<Integer, Integer> fib = new HashMap<>();
6456

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) {
6860
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);
7263
}
7364
fib.put(i, f);
7465
}
7566

7667
return fib.get(n);
7768
}
78-
79-
80-
69+
70+
8171
/**
8272
* This method finds the nth fibonacci number using bottom up
8373
*
84-
* @author Shoaib Rayeen (https://github.com/shoaibrayeen)
8574
* @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)
9484
**/
9585
private static int fibOptimized(int n) {
96-
9786
if (n == 0) {
9887
return 0;
9988
}
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;
10594
}
10695
return res;
10796
}
108-
}
109-
97+
}

0 commit comments

Comments
 (0)