Posted on 2023-01-30 13:59
Uriel 阅读(39)
评论(0) 编辑 收藏 引用 所属分类:
闲来无事重切Leet Code 、
大水题
求Tribonacci数列的第n位,大水题
1 #1137
2 #Runtime: 26 ms (Beats 27.25%)
3 #Memory: 13.5 MB (Beats 38.88%)
4
5 class Solution(object):
6 def tribonacci(self, n):
7 """
8 :type n: int
9 :rtype: int
10 """
11 if n == 0:
12 return 0
13 if n == 1 or n == 2:
14 return 1
15 t0 = 0
16 t1, t2 = 1, 1
17 for i in range(3, n + 1):
18 t = t0 + t1 + t2
19 t0 = t1
20 t1 = t2
21 t2 = t
22 return t