剑指 Offer 10- I. 斐波那契数列
题目
斐波那契数列
C++代码
class Solution {
public:
int fib(int n){
if(n < 1)
return 0;
if(n == 1)
return 1;
if(n == 2)
return 1;
int a = 1, b = 1;
for(int i = 3; i <= n; i++){
int tmp = a + b;
a = b;
b = tmp % 1000000007;
}
return b % 1000000007;
}
};
Java代码
class Solution {
共有 0 条评论