public static void main(String[] args) {
System.out.println(fib2(6));
}
//原始递归
private static int fib(int n) {
if (n==1)return 1;
if (n==0)return 0;
return fib(n-1)+fib(n-2);
}
//优化
private static int fib2(int n){
if (n==1)return 1;
if (n==0)return 0;
int low=0,high=1;
for (int i = 2; i <= n; i++) {
int sum=low+high;
low=high;
high=sum;
}
return high;
}
8.斐波那契数列优化
于 2022-04-09 08:35:38 首次发布