Title Description
We all know that Fibonacci number, and now asked to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0).
Ideas analysis
- Recursive thinking, there will be a lot of duplication of operations, inefficiency;
- Can be calculated from the bottom up, is first calculated according to f (0) and f (1) f a (2), and then (2) to calculate f according to f (1), and f (. 3), a analogy can be calculated by first n terms. Time complexity of the algorithm is O (n);
Java code
public class Offer010_1 {
public static void main(String[] args) {
System.out.println(Fibonacci(8));
}
public static int Fibonacci(int n) {
return Solution2(n);
}
/**
* 递归的思路
* @param n
* @return
*/
private static int Solution1(int n) {
if(n<=0) {
return 0;
}
if(n==1) {
return 1;
}
return Solution1(n-1)+Solution1(n-2);
}
/**
* 把计算过的只保存起来,保存前面的两项,计算的过程中给前面两项重新赋值
* @param n
* @return
*/
private static int Solution2(int n) {
int[] arr = {0,1};
if(n<2) {
return arr[n];
}
int fm1 = 1;// 当前数 前面的第1个数
int fm2 = 0;//当前数的前面的第2个数
int fN =0;
for(int i=2;i<=n;++i) {
fN = fm1+fm2;
fm2 = fm1;
fm1 = fN;
}
return fN;
}
}