# 1137. N-th Tribonacci Number

The Tribonacci sequence Tn is defined as follows:

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

Given `n`, return the value of Tn.

Example 1:

``````Input: n = 4
Output: 4
Explanation:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4
``````

Example 2:

``````Input: n = 25
Output: 1389537
``````

Constraints:

• `0 <= n <= 37`
• The answer is guaranteed to fit within a 32-bit integer, ie. `answer <= 2^31 - 1`.

``````class Solution {
public:
int tribonacci(int n) {
if (n < 2) return n;
int first = 0, second = 1, third = 1;
for (int i = 2; i < n; ++i) {
int t = first;
first = second;
second = third;
third = t + first + second;
}
return third;
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1137

Climbing Stairs

Fibonacci Number

https://leetcode.com/problems/n-th-tribonacci-number/

https://leetcode.com/problems/n-th-tribonacci-number/discuss/345236/JavaC%2B%2BPython-Easy-and-Concise

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation