1137. 第 N 个泰波那契数
class Solution { public int tribonacci(int n) { int T0 = 0; int T1 = 1; int T2 = 1; int arr[] = new int [40]; arr[0] = 0; arr[1] = 1; arr[2] = 1; for(int i = 3 ; i <= n ; i++) { arr[i] = arr[i-2]+ arr[i-1]+arr[i-3]; } return arr[n]; } }
有一种更省空间 不用开一个array
class Solution { public int tribonacci(int n) { if (n==0) return 0; int T1 = 0; int T2 = 1; int T3 = 1; for (int i = 4; i <= n+1; i++) { int T4 = T1 + T2 + T3; T1 = T2; T2 = T3; T3 = T4; } return T3; } }