剑指Offer:7.斐波那契数列

题目描述
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
n<=39

public class Solution {
    public int Fibonacci(int n) {
        //自顶向下备忘录法动态规划
        if(n==0)return 0;
        else if(n==1)return 1;
        else{
        int []memo=new int[n+1];
        memo[0]=0;
        memo[1]=1;
        for(int i=2;i<=n;i++)
            memo[i]=memo[i-1]+memo[i-2];
        return memo[n];
    }
  }
}

猜你喜欢

转载自blog.csdn.net/waS_TransvolnoS/article/details/95318936