65. Fibonacci sequence

 

Title description

Everyone knows the Fibonacci sequence. Now it is required to input an integer n. Please output the nth term of the Fibonacci sequence (starting from 0, the 0th term is 0, and the 1st term is 1).

n \ leq 39n≤39

Example 1

enter

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

 

Guess you like

Origin blog.csdn.net/xiao__jia__jia/article/details/113528929