斐波那契数列
输入一个整数 n ,求斐波那契数列的第 n 项。
假定从0开始,第0项为0。(n<=39)
样例 输入整数 n=5
返回 5
class Solution(object):
def Fibonacci(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0: return 0
a = 0
b = 1
for i in range(2, n+1):
a, b = b, a + b
return b
生成器
生成器讲解
class Solution(object):
def Fibonacci(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0: return 0
def f(n):
a = 0
b = 1
for i in range(n):
yield b # 生成器
a, b = b, a + b
res = 0
for it in f(n):
res = it
return res