fibonacci

注意不能用迭代,会超时

python:

 1 # -*- coding:utf-8 -*-
 2 class Solution:
 3     def Fibonacci(self, n):
 4         # write code here
 5         lst = [0,1]
 6         if n<2:
 7             return lst[n]
 8         while n>len(lst)-1:
 9             lst.append(lst[-1]+lst[-2])
10         return lst[n]

c++

 1 class Solution {
 2 public:
 3     int Fibonacci(int n) {
 4         deque<int> que;
 5         que.push_back(0);
 6         que.push_back(1);
 7         if(n<2) return que[n];
 8         while(n>=2){
 9             que.push_back(que[0]+que[1]);
10             que.pop_front();
11             n--;
12         }
13         return que[1];
14     }
15 };

 

posted @ 2019-07-01 10:39  Austin_anheqiao  阅读(173)  评论(0编辑  收藏  举报