Ad
Code
Diff
  • import java.util.HashMap;
    import java.util.Map;
    
    public class Fibonacci {
    	public static long  calcFib(int n,int a,int b){
                return n == 1 ? a : calcFib(n-1,b,a+b);
       }
            
      public static long calcFibo(int n) {
        return calcFib(n,1,1);
      }
    }
    • import java.util.HashMap;
    • import java.util.Map;
    • public class Fibonacci {
    • private static Map<Integer, Long> memoizationMap = new HashMap<>();
    • public static long calcFibo(int n) {
    • //SECOND
    • if (n <= 1) {
    • return n;
    • }
    • if (memoizationMap.containsKey(n)) {
    • return memoizationMap.get(n);
    • }
    • long fiboValue = calcFibo(n - 1) + calcFibo(n - 2);
    • memoizationMap.put(n, fiboValue);
    • return fiboValue;
    • }
    • public static long calcFib(int n,int a,int b){
    • return n == 1 ? a : calcFib(n-1,b,a+b);
    • }
    • public static long calcFibo(int n) {
    • return calcFib(n,1,1);
    • }
    • }