package Recursion;
public class Tribonacci_1137 {
// 1137. N-th Tribonacci Number
/* 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.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/n-th-tribonacci-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。*/
public static void main(String[] args) {
Tribonacci_1137 t=new Tribonacci_1137();
System.out.println(t.tribonacci(25));
}
int x=2;
int T_0=0;
int T_1=1;
int T_2=1;
public int tribonacci(int n) {
if(n==0) {
return T_0;
}
if(n==1) {
return T_1;
}
if(n==2) {
return T_2;
}
recur(n);
return T_2;
}
private void recur(int n) {
if(x==n) {
return;
}
recur(--n);
int tmp=T_2;
T_2=T_0+T_1+T_2;
T_0=T_1;
T_1=tmp;
}
}
私念Moposion
发布了79 篇原创文章 · 获赞 7 · 访问量 8256
私信
关注