Lintcode--3(366)--斐波那契数列
生活随笔
收集整理的這篇文章主要介紹了
Lintcode--3(366)--斐波那契数列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:查找斐波納契數列中第 N 個數。
? ? ? ? ? 所謂的斐波納契數列是指:
? ? ? ? ? 前2個數是 0 和 1 。
? ? ? ? ? 第 i 個數是第 i-1 個數和第i-2 個數的和。
? ? ? ? ? 斐波納契數列的前10個數字是:
? ? ? ? ? 所謂的斐波納契數列是指:
? ? ? ? ? 前2個數是 0 和 1 。
? ? ? ? ? 第 i 個數是第 i-1 個數和第i-2 個數的和。
? ? ? ? ? 斐波納契數列的前10個數字是:
? ? ? ? ? 0,1,1,2,3,5,8,13,21...
程序:
class Solution { public: /* * @param n: an integer * @return: an ineger f(n) */int fibonacci(int n) { // write your code here vector<int> nums; nums.push_back(0); nums.push_back(1); for(int i=2; i<=n; i++)//小于等于 { nums.push_back(nums[i-1]+nums[i-2]); }return nums[n-1];//題目要求從1計數,而數組都從0計數 } };總結
以上是生活随笔為你收集整理的Lintcode--3(366)--斐波那契数列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现两个数的交换(异或,加减)
- 下一篇: Lintcode--2(56)--两数之