[头条刷题] 斐波那切数列



    int fib(const int num){
        std::vector<int> fibs({0, 1});

        if( num == 0 || num == 1){
            return num;
        }
        else{
            int len = fibs.size();
            int elem = 0;
            while( len - 1 < num){
                int las_pos = len - 1;
                int s_las_pos = las_pos - 1;
                elem = fibs[s_las_pos] + fibs[las_pos];
                fibs.push_back(elem);
                len = fibs.size();
            }
            return elem;
        }
    }


posted @ 2020-04-24 21:23  一条图图犬  阅读(141)  评论(0编辑  收藏  举报