65.フィボナッチ数列

 

タイトル説明

誰もがフィボナッチ数列を知っています。整数nを入力する必要があります。フィボナッチ数列のn番目の項を出力してください(0から始まり、0番目の項は0、1番目の項は1)。

n \leq39n≤39

例1

入る

4
返回值
3
代码实现:
public class Solution {
    public int Fibonacci(int n) {
        if (n == 0) return n;
        int first = 0;
        int two = 1;
        for (int i = 2; i <= n; i++) {
            int tmp = first + two;
            first = two;
            two = tmp;
        }
        return two;
    }
}

 

おすすめ

転載: blog.csdn.net/xiao__jia__jia/article/details/113528929