1、简单递归思想
class Solution {
public int fib(int n) {
int num = 0;
for(int i =0;i<=n;i++){
num = fin(i);
}
return num ;
}
public static int fin(int number){
if(number == 0 || number == 1 ){
return number;
}else{
return (fin(number-1) + fin(number-2));
}
}
}
2、常规思想
class Solution {
public int fib(int n) {
if(n<2){
return n;
}
int p = 0,q =0,r=1;
for(int i = 2;i<=n;i++){
p = q;
q = r;
}
return r;
}
}
算法尚未成功,同志仍需努力。
剑指offer。