fix: update Fibonacci and close #1008

* close #1008
This commit is contained in:
Yang Libin
2019-10-24 14:38:08 +08:00
committed by GitHub
parent b0f81f1bc9
commit c502da807c

View File

@ -1,9 +1,8 @@
package DynamicProgramming; package DynamicProgramming;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Scanner;
/** /**
* @author Varun Upadhyay (https://github.com/varunu28) * @author Varun Upadhyay (https://github.com/varunu28)
@ -13,14 +12,15 @@ public class Fibonacci {
private static Map<Integer, Integer> map = new HashMap<>(); private static Map<Integer, Integer> map = new HashMap<>();
public static void main(String[] args) throws Exception { public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...] // Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(fibMemo(n)); System.out.println(fibMemo(n));
System.out.println(fibBotUp(n)); System.out.println(fibBotUp(n));
System.out.println(fibOptimized(n));
} }
/** /**
@ -29,7 +29,7 @@ public class Fibonacci {
* @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) { public static int fibMemo(int n) {
if (map.containsKey(n)) { if (map.containsKey(n)) {
return map.get(n); return map.get(n);
} }
@ -51,7 +51,7 @@ public class Fibonacci {
* @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) { public static int fibBotUp(int n) {
Map<Integer, Integer> fib = new HashMap<>(); Map<Integer, Integer> fib = new HashMap<>();
@ -83,7 +83,7 @@ public class Fibonacci {
* 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) * @author Shoaib Rayeen (https://github.com/shoaibrayeen)
**/ **/
private static int fibOptimized(int n) { public static int fibOptimized(int n) {
if (n == 0) { if (n == 0) {
return 0; return 0;
} }