赞
踩
泰波那契序列 Tn 定义如下:
T
0
=
0
,
T
1
=
1
,
T
2
=
1
T_0 = 0, T_1 = 1, T_2 = 1
T0=0,T1=1,T2=1, 且在 n >= 0 的条件下
T
n
+
3
=
T
n
+
T
n
+
1
+
T
n
+
2
T_{n+3} = T_n + T_{n+1} + T_{n+2}
Tn+3=Tn+Tn+1+Tn+2
给你整数 n,请返回第 n 个泰波那契数 Tn 的值。
示例 1:
输入:n = 4
输出:4
解释:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4
示例 2:
输入:n = 25
输出:1389537
提示:
class Solution {
public int tribonacci(int n) {
int a = 0, b = 1, c = 1;
if(n == 0) return 0;
if(n == 1 || n == 2) return 1;
int ans = 0;
for(int i = 2; i < n; i++){
ans = a + b + c;
a = b;
b = c;
c = ans;
}
return ans;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。