LeetCode(力扣)509. 斐波那契数Python

LeetCode509. 斐波那契数

题目链接

https://leetcode.cn/problems/fibonacci-number/
在这里插入图片描述

代码

class Solution:
    def fib(self, n: int) -> int:
        if n == 0:
            return 0
        dp = [0] * (n + 1)

        dp[0] = 0
        dp[1] = 1
    
        for i in range(2, n + 1):
            dp[i] = dp[i - 1] + dp[i - 2]
        return dp[n]
class Solution:
    def fib(self, n: int) -> int:
        if n < 2:
            return n
        return self.fib(n - 1) + self.fib(n - 2)

猜你喜欢

转载自blog.csdn.net/qq_44953660/article/details/132997443