[入门]斐波纳契数列

题目来源:http://www.lintcode.com/zh-cn/problem/fibonacci/

一开始用递归的方法,error,超时。

i<=n-2,返回c,error。

 1 class Solution{
 2 public:
 3     /**
 4      * @param n: an integer
 5      * @return an integer f(n)
 6      */
 7     int fibonacci(int n) {
 8         // write your code here
 9        int a=0,b=1;
10        int c;
11        for(int i = 1; i<=n-1; i++) {
12            c = a + b;
13            a = b;
14            b = c;
15        }
16        return a;
17     }
18 };
posted @ 2016-04-28 22:39  Pearl_zju  阅读(217)  评论(0编辑  收藏  举报