我有2个班级:RecursiveFibonacci和MemorizedRecursiveFibonacci。到目前为止,这就是我所拥有的。
递归斐波那契类
public class SimpleRecursiveFibonacci {
public BigInteger fibonacci(int n) {
if(n < 2) {
return BigInteger.ONE;
}
return fibonacci(n - 2).add(fibonacci(n - 1));
}
}
和“记忆递归斐波那契”类
public class MemoizedRecursiveFibonacci {
private Map<Integer, BigInteger> cache = new HashMap<>();
public BigInteger fibonacci(int n) {
if(n < 2) {
return BigInteger.ONE;
}
if(!cache.containsKey(n)){
BigInteger currentFibonacci = fibonacci(n - 2).add(fibonacci(n - 1));
cache.put(n, currentFibonacci);
}
return cache.get(n);
}
}
如我所见,MemorizedRecursiveFibonacci类中有一些重复的代码
if(n < 2) {
return BigInteger.ONE;
和
BigInteger currentFibonacci = fibonacci(n - 2).add(fibonacci(n - 1));
如何使其保持干燥?删除重复的代码?
答案 0 :(得分:6)
也许这是一个选择...但是我认为不是最好的。
public class SimpleRecursiveFibonacci {
public BigInteger fibonacci(int n) {
if(n < 2) {
return BigInteger.ONE;
}
return calculate(n);
}
protected BigInteger calculate(int n){
return fibonacci(n - 2).add(fibonacci(n - 1)),
}
}
public class MemoizedRecursiveFibonacci extends SimpleRecursiveFibonacci{
private Map<Integer, BigInteger> cache = new HashMap<>();
@Override
protected BigInteger calculate(int n) {
if(!cache.containsKey(n)){
BigInteger currentFibonacci = super.calculate(n);
cache.put(n, currentFibonacci);
}
return cache.get(n)
}
}
答案 1 :(得分:6)
怎么样呢?
public class SimpleRecursiveFibonacci {
/** Gets the fibonacci value for n */
public final BigInteger fibonacci(int n) {
if (n == 0) {
return BigInteger.ZERO;
} else if (n == 1) {
return BigInteger.ONE;
}
return getFibonacci(n);
}
/** Recursively calculates the fibonacci by adding the two previous fibonacci. */
protected final BigInteger calculateFibbonacci(int n) {
return fibonacci(n - 2).add(fibonacci(n - 1));
}
/**
* Somehow get the fibonacci value for n.
* Could be by calculation, getting it from a cache, or anything.
* Defaults to calculation.
*/
protected BigInteger getFibonacci(int n) {
return calculateFibbonacci(n);
}
}
public class MemoizedRecursiveFibonacci extends SimpleRecursiveFibonacci {
// Cache using an array list as recommended by user @DodgyCodeException
private ArrayList<BigInteger> cache = new ArrayList<>();
@Override
protected BigInteger getFibonacci(int n) {
if (cache.size() < n) {
BigInteger fib = calculateFibbonacci(n);
cache.add(fib);
return fib;
} else {
return cache.get(n - 1);
}
}
}
答案 2 :(得分:3)
另一个使用Java 8功能的示例-具有lambda表达式的BiFunction
接口:
BiFunction<Fibonacci, Integer, BigInteger> func = (fibonacci, n) -> {
if (n < 2) {
return BigInteger.ONE;
}
return fibonacci.calc(n - 2).add(fibonacci.calc(n - 1));
};
new CachedFibonacci(func).calc(100);
实施:
interface Fibonacci {
BigInteger calc(int n);
}
class SimpleFibonacci implements Fibonacci {
private BiFunction<Fibonacci, Integer, BigInteger> fibonacci;
SimpleFibonacci(BiFunction<Fibonacci, Integer, BigInteger> fibonacci) {
this.fibonacci = fibonacci;
}
public BigInteger calc(int n) {
return fibonacci.apply(this, n);
}
}
class CachedFibonacci implements Fibonacci {
private BiFunction<Fibonacci, Integer, BigInteger> fibonacci;
private Map<Integer, BigInteger> cache = new HashMap<>();
CachedFibonacci(BiFunction<Fibonacci, Integer, BigInteger> fibonacci) {
this.fibonacci = fibonacci;
}
public BigInteger calc(int n) {
if (!cache.containsKey(n)) {
cache.put(n, fibonacci.apply(this, n));
}
return cache.get(n);
}
}
答案 3 :(得分:-2)
使用静态方法:
public class SimpleRecursiveFibonacci {
public static BigInteger fibonacci(int n) {
if(n < 2) {
return BigInteger.ONE;
}
return fibonacci(n - 2).add(fibonacci(n - 1));
}
}
public class MemoizedRecursiveFibonacci {
private Map<Integer, BigInteger> cache = new HashMap<>();
public BigInteger fibonacci(int n) {
if(!cache.containsKey(n)){
BigInteger currentFibonacci = SimpleRecursiveFibonacci.fibonacci(n);
cache.put(n, currentFibonacci);
}
return cache.get(n);
}
}