Skip to content

509. 斐波那契数

Published: at 23:48

爬楼梯稍微改一下初始参数。

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

Previous Post
70. 爬楼梯
Next Post
1137. 第 N 个泰波那契数