1137. N-th Tribonacci Number(遞歸)

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;
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章