【Leetcode_总结】509. 斐波那契数 - python

Q:

斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:

F(0) = 0,   F(1) = 1
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.

链接:https://leetcode-cn.com/problems/fibonacci-number/description/

思路:递推求解

代码:

class Solution(object):
    def fib(self, N):
        """
        :type N: int
        :rtype: int
        """
        if N==0: return 0
        pre = 1
        ppre = 0
        for i in range(1,N):
            pre,ppre =  pre+ppre,pre
        return pre

 

你可能感兴趣的:(Leetcode,数组)