题解 | #斐波那契数列#
斐波那契数列
http://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3
# 动态规划,空间复杂度O(1),时间复杂度O(n)
class Solution:
def Fibonacci(self , n: int) -> int:
if n <= 2:
return 1
# a和b分别为保存的前两位
a = b = 1
while n >= 3:
# tmp为当前和,更新前两位
tmp = a + b
a = b
b = tmp
n = n - 1
return b