mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-14 01:16:07 +08:00
fix: remove unnecesary assignation to fix #698
- Fix #698 - Thanks @lprone
This commit is contained in:
@ -4,14 +4,13 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||||
*
|
* @author yanglbme (https://github.com/yanglbme)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class Fibonacci {
|
public class Fibonacci {
|
||||||
|
|
||||||
private static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
|
private static Map<Integer, Integer> map = new HashMap<>();
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
@ -26,13 +25,8 @@ public class Fibonacci {
|
|||||||
* This method finds the nth fibonacci number using memoization technique
|
* This method finds the nth fibonacci number using memoization technique
|
||||||
*
|
*
|
||||||
* @param n The input n for which we have to determine the fibonacci number
|
* @param n The input n for which we have to determine the fibonacci number
|
||||||
* Outputs the nth fibonacci number
|
* Outputs the nth fibonacci number
|
||||||
**/
|
**/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static int fibMemo(int n) {
|
private static int fibMemo(int n) {
|
||||||
if (map.containsKey(n)) {
|
if (map.containsKey(n)) {
|
||||||
return map.get(n);
|
return map.get(n);
|
||||||
@ -42,10 +36,9 @@ public class Fibonacci {
|
|||||||
|
|
||||||
if (n <= 2) {
|
if (n <= 2) {
|
||||||
f = 1;
|
f = 1;
|
||||||
}
|
} else {
|
||||||
else {
|
f = fibMemo(n - 1) + fibMemo(n - 2);
|
||||||
f = fibMemo(n-1) + fibMemo(n-2);
|
map.put(n, f);
|
||||||
map.put(n,f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return f;
|
return f;
|
||||||
@ -55,55 +48,50 @@ public class Fibonacci {
|
|||||||
* This method finds the nth fibonacci number using bottom up
|
* This method finds the nth fibonacci number using bottom up
|
||||||
*
|
*
|
||||||
* @param n The input n for which we have to determine the fibonacci number
|
* @param n The input n for which we have to determine the fibonacci number
|
||||||
* Outputs the nth fibonacci number
|
* Outputs the nth fibonacci number
|
||||||
**/
|
**/
|
||||||
|
|
||||||
private static int fibBotUp(int n) {
|
private static int fibBotUp(int n) {
|
||||||
|
|
||||||
Map<Integer,Integer> fib = new HashMap<Integer,Integer>();
|
Map<Integer, Integer> fib = new HashMap<>();
|
||||||
|
|
||||||
for (int i=1;i<n+1;i++) {
|
for (int i = 1; i < n + 1; i++) {
|
||||||
int f = 1;
|
int f;
|
||||||
if (i<=2) {
|
if (i <= 2) {
|
||||||
f = 1;
|
f = 1;
|
||||||
}
|
} else {
|
||||||
else {
|
f = fib.get(i - 1) + fib.get(i - 2);
|
||||||
f = fib.get(i-1) + fib.get(i-2);
|
|
||||||
}
|
}
|
||||||
fib.put(i, f);
|
fib.put(i, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fib.get(n);
|
return fib.get(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method finds the nth fibonacci number using bottom up
|
* This method finds the nth fibonacci number using bottom up
|
||||||
*
|
*
|
||||||
* @author Shoaib Rayeen (https://github.com/shoaibrayeen)
|
|
||||||
* @param n The input n for which we have to determine the fibonacci number
|
* @param n The input n for which we have to determine the fibonacci number
|
||||||
* Outputs the nth fibonacci number
|
* Outputs the nth fibonacci number
|
||||||
*
|
* <p>
|
||||||
* This is optimized version of Fibonacci Program. Without using Hashmap and recursion.
|
* This is optimized version of Fibonacci Program. Without using Hashmap and recursion.
|
||||||
* It saves both memory and time.
|
* It saves both memory and time.
|
||||||
* Space Complexity will be O(1)
|
* Space Complexity will be O(1)
|
||||||
* Time Complexity will be O(n)
|
* Time Complexity will be O(n)
|
||||||
*
|
* <p>
|
||||||
* Whereas , the above functions will take O(n) Space.
|
* Whereas , the above functions will take O(n) Space.
|
||||||
|
* @author Shoaib Rayeen (https://github.com/shoaibrayeen)
|
||||||
**/
|
**/
|
||||||
private static int fibOptimized(int n) {
|
private static int fibOptimized(int n) {
|
||||||
|
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int prev = 0 , res = 1 , next;
|
int prev = 0, res = 1, next;
|
||||||
for ( int i = 2; i < n; i++) {
|
for (int i = 2; i < n; i++) {
|
||||||
next = prev + res;
|
next = prev + res;
|
||||||
prev = res;
|
prev = res;
|
||||||
res = next;
|
res = next;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user