思路:
开三个变量存三个数,下一次迭代first的时候是上一轮的mid 和 end
代码:
class Solution {
public:
int fib(int n) {
int MOD = 1e9+7;
if (n < 2) {
return n;
}
int first = 0, mid = 0, end = 1;
for (int i = 2; i <= n; ++i) {
first = mid;
mid = end;
end = (first + mid)%MOD;
}
return end;
}
};
评论区