剑指offer:07 斐波那契数列

题目要求

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

Python

class Solution:

    def Fibonacci(self, n):

        # write code here

        if n == 0:

            return 0

        if n == 1:

            return 1

        a, b = 0, 1

        for _ in range( n-2 ):

            a, b = b, a + b

        return (a+b)

你可能感兴趣的:(剑指offer:07 斐波那契数列)