Skip to content

1137. 第 N 个泰波那契数

Published: at 23:51

斐波那契的小改动。

impl Solution {
pub fn tribonacci(n: i32) -> i32 {
if n == 0 {
return 0;
} else if n == 1 {
return 1;
} else if n == 2 {
return 1;
}
let mut data = vec![0, 1, 1];
let mut next = 0;
let mut i = 2;
while i < n {
data[next] = data[0] + data[1] + data[2];
i += 1;
if i == n {
return data[next];
}
next = (next + 1) % 3;
}
data[next]
}
}

Previous Post
509. 斐波那契数
Next Post
746. 使用最小花费爬楼梯