leetcode
C++:
class Solution {
public:
int fib(int n) {
if ( n < 2 ) {
return n;
}
int prepre = 0;
int pre = 1;
int i = 2;
while ( i < n ) {
int temp = pre % 1000000007;
pre = ( prepre % 1000000007 + pre % 1000000007 ) % 1000000007;
prepre = temp;
++i;
}
return ( prepre % 1000000007 + pre % 1000000007 ) % 1000000007;
}
};
网友评论