[剑指 Offer 10- I. 斐波那契数列]
题目链接
https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/
很基本的迭代题,用不到dp或递归
int fib(int n){
if(n<=1)
return n;
int fir = 0;
int sec = 1;
int tmp;
while(n>1){
tmp = (fir + sec)%1000000007;
fir = sec;
sec = tmp;
n--;
}
return sec;
}
时间复杂度 O(n)
空间复杂度 O(1)