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

LeetCode509. 斐波那契数

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/fibonacci-number/
LeetCode(力扣)509. 斐波那契数Python_第1张图片

代码

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)

你可能感兴趣的:(leetcode,python,算法,职场和发展)