力扣-1137.第N个泰波那契数

力扣-1137.第N个泰波那契数_第1张图片

class Solution(object):
    def tribonacci(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n == 1 or n == 0:
            return n
        if n == 2:
            return 1
        dp_1 = 1
        dp_2 = 1
        dp_3 = 0

        for i in range(3, n + 1):
            res = dp_1 + dp_2 + dp_3
            dp_3 = dp_2
            dp_2 = dp_1
            dp_1 = res
            i = i + 1
        return dp_1


if __name__ == '__main__':
    n = 25
    Sol = Solution()
    res = Solution.tribonacci(Sol, n)
    print(res)

你可能感兴趣的:(学习笔记,python)