剑指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;
    }
}

猜你喜欢

转载自www.cnblogs.com/buptyuhanwen/p/9376951.html
今日推荐