斐波那契数(Leetcode 509)给定 N,计算 F(N)。
F(0) = 0, F(1) = 1
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
class Solution:
def Fibonacci(self, n):
if n <= 1:
return n
res = [0, 1]
for i in range(2, n + 1):
res.append(res[-1] + res[-2]) # 动态规划
return res[n]