题目:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。n<=39
知识点:斐波那契数列的规律为后一项的值等于前两项的和,如0,1,1,2,3,5,8,13…
思路:变量迭代替换
python代码:
class Solution:
def Fibonacci(self, n):
# write code here
a = 0
b = 1
i = 2
if n0:
return 0
elif n1:
return 1
else:
while i<=n:
c = b
b = a+b
a = c
i += 1
return b