题意
分析
简单的动态规划,使用两个临时变量保存。
代码
public class Solution {
public int Fibonacci(int n) {
if(n<=0)return 0;
if(n==1||n==2)return 1;
int index = 3;
int pre=1;
int last=1;
int temp = last;
while(index<=n){
temp = last;
last = pre+last;
pre = temp;
index++;
}
return last;
}
}