Find the Nth number in Fibonacci sequence.

A Fibonacci sequence is defined as follow:

  • The first two numbers are 0 and 1.
  • The i th number is the sum of i-1 th number and i-2 th number.

The first ten numbers in Fibonacci sequence is:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...

Notice

The Nth fibonacci number won't exceed the max value of signed 32-bit integer in the test cases.

 
Example

Given 1, return 0

Given 2, return 1

Given 10, return 34

题意

查找斐波纳契数列中第 N 个数。

所谓的斐波纳契数列是指:

  • 前2个数是 0 和 1 。
  • 第 i 个数是第 i-1 个数和第i-2 个数的和。

解法一:

 class Solution {
public:
/*
* @param n: an integer
* @return: an ineger f(n)
*/
int fibonacci(int n) {
int a, b;
a = ;
b = ; for (int i = ; i < n; i++) {
int c = a + b;
a = b;
b = c;
}
return a;
}
};

解法二:

 class Solution {
public:
/*
* @param n: an integer
* @return: an ineger f(n)
*/
int fibonacci(int n) {
if (n == ) {
return ;
} else if (n == ) {
return ;
} return fibonacci(n - ) + fibonacci(n - ); }
};

递归会超时

 

366. Fibonacci【Naive】的更多相关文章

  1. 219. Insert Node in Sorted Linked List【Naive】

    Insert a node in a sorted linked list. Example Given list = 1->4->6->8 and val = 5. Return  ...

  2. 452. Remove Linked List Elements【Naive】

    Remove all elements from a linked list of integers that have value val. Example Given 1->2->3- ...

  3. 466. Count Linked List Nodes【Naive】

    Count how many nodes in a linked list. Example Given 1->3->5, return 3. 解法一: /** * Definition ...

  4. 632. Binary Tree Maximum Node【Naive】

    Find the maximum node in a binary tree, return the node. Example Given a binary tree: 1 / \ -5 2 / \ ...

  5. 【HDU1848】Fibonacci again and again(博弈论)

    [HDU1848]Fibonacci again and again(博弈论) 题面 Hdu 你有三堆石子,每堆石子的个数是\(n,m,p\),你每次可以从一堆石子中取走斐波那契数列中一个元素等数量的 ...

  6. 【CF914G】Sum the Fibonacci 快速??变换模板

    [CF914G]Sum the Fibonacci 题解:给你一个长度为n的数组s.定义五元组(a,b,c,d,e)是合法的当且仅当: 1. $1\le a,b,c,d,e\le n$2. $(s_a ...

  7. 【HDU3117】Fibonacci Numbers

    [HDU3117]Fibonacci Numbers 题面 求斐波那契数列的第\(n\)项的前四位及后四位. 其中\(0\leq n<2^{32}\) 题解 前置知识:线性常系数齐次递推 其实后 ...

  8. HDU 1568 Fibonacci【求斐波那契数的前4位/递推式】

    Fibonacci Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Proble ...

  9. POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17171   Accepted: 11999 Descr ...

随机推荐

  1. github pages+hexo自建博客

    1.github创建新项目,然后开启pages即可 2.全局安装hexo npm install -g hexo 3.初始化hexo hexo init 4.安装hexo的依赖 npm i 5.基本上 ...

  2. OpenCV学习(4) Mat的基本操作(1)

          图像在OpenCV中都是通过Mat类来存储的,Mat可以用来表示N维矩阵,当然用的最多的还是二维矩阵.       Mat类有两部分组成:第一部分是头信息,这些信息主要用来描述矩阵,比如矩 ...

  3. JavaScript实现计算器功能

    截图 : cal.js var Class = {} ; Class.calculation = function(){ var calculation = {} ; calculation.resu ...

  4. C#和Java交互相关研究

    之前总觉得C#和Java可以交互应用,但是由于时间以及其他方面的原因,一直没有调研.今天抽空搜了一下,终于折腾出来了. 以下是我自己就C#和Java整合的一些提问和分析,如果有不对的地方,请路过的各位 ...

  5. hadoop中的序列化与Writable类

    本文地址:http://www.cnblogs.com/archimedes/p/hadoop-writable-class.html,转载请注明源地址. hadoop中自带的org.apache.h ...

  6. 【Python】Django filter 如何支持 or 条件过滤?

    from django.db.models import Q Item.objects.filter(Q(creator=owner) | Q(moderated=False)) 代码示例: if(r ...

  7. 转: SVN和Git的一些用法总结

    转:http://www.codelast.com/?p=5719 转载请注明出处:http://www.codelast.com/ 以下都是比较基础的操作,高手们请绕道,不必浪费时间来看了. (A) ...

  8. (算法)二叉树的第m层第k个节点

    题目: 给定以下二叉树: struct node { node *left, *right; int value; }; 要求编写函数 node* foo(node *node, unsigned i ...

  9. 淘宝网前端开发面试题(一)--HTML & CSS 面试题

    所有答案仅供参考,不负责答案对错(^_^) 1.DOCTYPE? 严格模式不混杂模式-如何触发这两种模式,区分它们有何意义? 分析: DOCTYPE(是DOCument TYPE的缩写,即文档类型)是 ...

  10. android.content.res.Resources$NotFoundException: String resource ID #0x0

    仔细检查是不是在settext的时候设置进去的时int属性的值,所以android会认为这是在strings中的值,所以会拿着这个int值当做string的id值去找,结果当然是找不到的.