509. Fibonacci Number

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

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

Given N, calculate F(N).

斐波那契数列,按照递推公式来就行

class Solution(object):
    def fib(self, N):
        """
        :type N: int
        :rtype: int
        """
        fa = [0, 1]
        for i in range(2, N + 1, 1):
            fa.append(fa[i - 1] + fa[i -2])
        return fa[N]

 

你可能感兴趣的:(509. Fibonacci Number)