# leetcode-dp-第N个泰波那契数(Leetcode DP – nth teponacci number)-其他

## leetcode-dp-第N个泰波那契数(Leetcode DP – nth teponacci number)

``````
/**
1137. 第 N 个泰波那契数

T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2

T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4

0 <= n <= 37

*/

public int tribonacci(int n) {
if(n==0){
return 0;
}

if (n == 1 || n == 2) {
return 1;
}

int[] dp = new int[n+1];
dp[0] = 0;
dp[1] = 1;
dp[2] = 1;
//如果用递归就会超时，用dp,递推公式  Tn+3 = Tn + Tn+1 + Tn+2，注意起始位置和特殊情况
for (int i = 3; i <= n; i++) {
dp[i]=dp[i-1]+ dp[i-2]+dp[i-3];
}
return dp[n];
}

``````
————————
``````
/**
1137. 第 N 个泰波那契数

T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2

T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4

0 <= n <= 37

*/

public int tribonacci(int n) {
if(n==0){
return 0;
}

if (n == 1 || n == 2) {
return 1;
}

int[] dp = new int[n+1];
dp[0] = 0;
dp[1] = 1;
dp[2] = 1;
//如果用递归就会超时，用dp,递推公式  Tn+3 = Tn + Tn+1 + Tn+2，注意起始位置和特殊情况
for (int i = 3; i <= n; i++) {
dp[i]=dp[i-1]+ dp[i-2]+dp[i-3];
}
return dp[n];
}

``````