package ch8;
import org.junit.Test;
/**
* Created by Administrator on 2016/4/11.
*/
public class Main2 {
/**
* 传统写法
* 说明:效率低
* 时间复杂度:2^n
*/
public long method1(int n) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return method1(n - 1) + method1(n - 2);
}
/**
* 加强版写法
* 时间复杂度:O(n)
* @param n
* @return
*/
public long method2(int n) {
if(n==0){
return 0;
}
int x = 1;
int y = 1;
int tmp;
for (int i = 2; i < n; i++) {
tmp = x;
x = y;
y = y + tmp;
}
return y;
}
@Test
public void test() {
System.out.println(method1(0));
System.out.println(method2(0));
}
}
转载于:https://www.cnblogs.com/ayhq/p/5392784.html