1137. 第 N 个泰波那契数 - 力扣(LeetCode) (leetcode-cn.com)
运行结果
代码
class Solution {
public:
int tribonacci(int n) {
int T[] = {0, 1, 1};
if(n < 3) return T[n];
int Tn_3 = 0, Tn_2 = 1, Tn_1 = 1, Tn;
for(int i = 3; i <= n; ++i){
Tn = Tn_1 + Tn_2 + Tn_3;
Tn_3 = Tn_2;
Tn_2 = Tn_1;
Tn_1 = Tn;
}
return Tn;
}
};