-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
47 lines (35 loc) · 924 Bytes
/
Fibonacci.java
File metadata and controls
47 lines (35 loc) · 924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package leetcode;
public class Fibonacci {
public static void main(String[] args) {
Fibonacci fibonacci = new Fibonacci();
//System.out.println(fibonacci.Fib(1));
int[] memo = new int[11];
System.out.println(fibonacci.Fib2(10, memo));
}
public int Fib(int n){
if (n==1){
return 1;
}
if (n==2){
return 1;
}
int result = Fib(n-1) + Fib(n-2);
return result;
}
public int Fib2(int n, int[] memo){
//memo = new int[n+1];
if (n == 0){
return n;
}
if (n ==1 || n ==2){
memo[n] = 1;
return 1;
}
if (memo[n] != 0){
return memo[n];
}
int result = Fib2(n-1, memo) + Fib2(n-2, memo);
memo[n] = result;
return result;
}
}