You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
599 B
Java
27 lines
599 B
Java
package _12._1;
|
|
|
|
import provided._12._1.Fibonacci;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class FibonacciDynamic extends Fibonacci {
|
|
protected Map<Integer, Long> map = new HashMap<>();
|
|
|
|
public FibonacciDynamic() {
|
|
this.map.put(0, (long) 0);
|
|
this.map.put(1, (long) 1);
|
|
}
|
|
|
|
@Override
|
|
public long calculate(int n) {
|
|
if (this.map.containsKey(n)) {
|
|
return this.map.get(n);
|
|
} else {
|
|
long result = calculate(n-1) + calculate(n-2);
|
|
this.map.put(n, result);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
}
|