天天看点

【leetcode刷题笔记】十六、斐波那契数

解题思路

这题就是对昨天题目的复习,类似这种递归思想的题都可以这么做。

代码

class Solution {
    public int fib(int n) {
        if(n==0){
            return 0;
        }
        if(n==1){
            return 1;
        }
        int a=0;
        int b=1;
        for(int i=2;i<=n;i++){
            int temp=a+b;
            a=b;
            b=temp;
        }
        return b;
    }
}