jz5.斐波那契额数列

165 阅读1分钟

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

f(0)=0
f(1)=1
f(n)=f(0)+f(1)+f(2)+...+f(n-1)
public class Solution {
    public int Fibonacci(int n) {
        if(n<2){
            return n;
        }
        int one=0;
        int two=1;
        int result=0;
        for(int i=2;i<=n;i++){
            result=one+two;
            one=two;
            two=result;
        }
        return result;

    }
}