剑指offer---07---动态规划:斐波那契数列

题意
 
分析
简单的动态规划,使用两个临时变量保存。
 
代码
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;
    }
}
posted @ 2018-07-27 12:37  buptyuhanwen  阅读(134)  评论(0编辑  收藏  举报