You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classSolution {
public:int mod = 1e9 + 7;
intfib(int n) {
if (n == 0) return0;
if (n == 1) return1;
int pre = 0;
int cur = 1;
int now;
for (int i = 2; i <= n; i++) {
now = (pre + cur)%mod;
pre = cur;
cur = now;
}
return now;
}
};
The text was updated successfully, but these errors were encountered:
写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N))。斐波那契数列的定义如下:
斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出。
答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。
示例 1:
示例 2:
提示:
递归会超时,记忆化搜索吧。代码如下:
The text was updated successfully, but these errors were encountered: