-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1137.第-n-个泰波那契数.py
64 lines (62 loc) · 1.06 KB
/
1137.第-n-个泰波那契数.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#
# @lc app=leetcode.cn id=1137 lang=python3
#
# [1137] 第 N 个泰波那契数
#
# https://leetcode.cn/problems/n-th-tribonacci-number/description/
#
# algorithms
# Easy (60.81%)
# Likes: 203
# Dislikes: 0
# Total Accepted: 124.3K
# Total Submissions: 204.4K
# Testcase Example: '4'
#
# 泰波那契序列 Tn 定义如下:
#
# T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2
#
# 给你整数 n,请返回第 n 个泰波那契数 Tn 的值。
#
#
#
# 示例 1:
#
# 输入:n = 4
# 输出:4
# 解释:
# T_3 = 0 + 1 + 1 = 2
# T_4 = 1 + 1 + 2 = 4
#
#
# 示例 2:
#
# 输入:n = 25
# 输出:1389537
#
#
#
#
# 提示:
#
#
# 0 <= n <= 37
# 答案保证是一个 32 位整数,即 answer <= 2^31 - 1。
#
#
#
# @lc code=start
class Solution:
def tribonacci(self, n: int) -> int:
if n == 0:
return 0
if n in [1,2 ]:
return 1
n = n-2
a,b,c = 0,1,1
while n :
a,b,c = b,c,a + b + c
n-=1
return c
# @lc code=end