定義
a0 = 0;
a1 = 1;
an = a(n-1)+a(n-2);
即數(shù)列:0窜护,1,1柱徙,2,3敌完,5羊初,8,13长赞,21,33得哆,54.......
Java遞歸實(shí)現(xiàn)
public int search(int n){
if(n<0){
return -1;
}else if(n == 0){
return 0;
}else if(n == 1){
return 1;
}else{
return search(n-1)+search(n-2);
}
}
Java非遞歸實(shí)現(xiàn)
public int searchWithoutTraversal(int n){
if(n< 0) {
return -1;
}
if(n == 0||n==1){
return n;
}else{
int result = 0;
int first = 0;
int second = 1;
for(int i= 2;i<n;i++){
result = first+ second;
first = second;
second = result;
}
return result;
}
}