Advertisement
SetKaung

Fib

Apr 13th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.28 KB | None | 0 0
  1. public static int fib(int n, ArrayList<Integer> arr){
  2.     if(arr.size() > n){
  3.         return arr.get(n);
  4.     }
  5.     if(n == 0){
  6.         return 0;
  7.     }
  8.     if(n == 1){
  9.         return 1;
  10.     }
  11.     int n2 = fib(n-2, arr);
  12.     arr[n-2] = n2;
  13.     int n1 = fib(n-1,arr);
  14.     return n1+n2;
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement