题解 | 斐波那契数列
斐波那契数列
https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param n int整型 # @return int整型 # class Solution: # def Fibonacci(self , n: int) -> int: # # write code here # if n==0: # return 0 # elif n == 1 or n == 2: # return 1 # else: # return self.Fibonacci(n-1) + self.Fibonacci(n-2) class Solution: def Fibonacci(self , n: int) -> int: # write code here function = {0:0, 1:1, 2:1} p, q = 0, 1 while q < n: if q >= 2: function[q+1] = function[p] + function[q] p = q q += 1 else: p = q q += 1 return function[n]