LeetCode //C - 1137. N-th Tribonacci Number

📅 2026/7/6 15:06:14
LeetCode //C - 1137. N-th Tribonacci Number
1137. N-th Tribonacci NumberThe Tribonacci sequenceT n T_nTn​is defined as follows:T 0 0 , T 1 1 , T 2 1 , a n d T n 3 T n T n 1 T n 2 f o r n 0. T_0 0, T_1 1, T_2 1, and T_{n3} T_n T_{n1} T_{n2} for n 0.T0​0,T1​1,T2​1,andTn3​Tn​Tn1​Tn2​forn0.Given n, return the value ofT n T_nTn​.Example 1:Input:n 4Output:4Explanation:T_3 0 1 1 2T_4 1 1 2 4Example 2:Input:n 25Output:1389537Constraints:0 n 37The answer is guaranteed to fit within a 32-bit integer, ie. answer 2^31 - 1.From: LeetCodeLink: 1137. N-th Tribonacci NumberSolution:Ideas:Iteratively calculate Tribonacci numbersusing the previous 3 values only.Code:inttribonacci(intn){if(n0)return0;if(n1||n2)return1;inta0,b1,c1;for(inti3;in;i){intdabc;ab;bc;cd;}returnc;}