Discuss / Java / 生成斐波那契数列

生成斐波那契数列

Topic source

Supplier 部分

class NatualSupplier implements Supplier<Integer> {
  private final int[] temp = new int[]{1, 1, 0};
  private int index = -1;

  @Override
  public Integer get() {
    index++;
    if (index < 2) {
      return temp[index];
    }
    return (temp[index % 3] = temp[(index - 1) % 3] + temp[(index - 2) % 3]);
  }
}

  • 1

Reply