forked from xtaci/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibo.java
More file actions
27 lines (22 loc) · 473 Bytes
/
Copy pathFibo.java
File metadata and controls
27 lines (22 loc) · 473 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
//cal fibo number using dynamic programming
public class Fibo {
private int [] array;
private int n;
public Fibo(int n) {
this.n = n;
this.array = new int[n];
}
public void printFibo(){
System.out.println(fib(n-1));
}
private int fib(int n) { // n-th Fibo number
if (n<=1) {
array[n] = 1;
} else if (array[n] == 0 && array[n-2] != 0) {
array[n] = array[n-1] + array[n-2];
} else {
array[n] = fib(n-1) + fib(n-2);
}
return array[n];
}
}