-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1137.py
40 lines (32 loc) · 960 Bytes
/
1137.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
1137. 第 N 个泰波那契数
泰波那契序列 Tn 定义如下:
T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2
给你整数 n,请返回第 n 个泰波那契数 Tn 的值。
"""
class Solution:
# 1.递归 但会超时
def tribonacci(self, n: int):
if n == 0:
return 0
if n == 1:
return 1
if n == 2:
return 1
return self.tribonacci(n - 1) + self.tribonacci(n - 2) + self.tribonacci(n - 3)
# 2 动态记忆
def __init__(self):
self.memo = {}
def tribonacci2(self, n: int) -> int:
if n == 0:
return 0
if n == 1 or n == 2:
return 1
try:
return self.memo[n]
except:
self.memo[n] = self.tribonacci(n - 1) + self.tribonacci(n - 2) + self.tribonacci(n - 3)
return self.memo[n]
s = Solution()
print(s.tribonacci(5))
print(s.tribonacci2(5))