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. mysql TO_DAYS()函数

    TO_DAYS(date)给定一个日期date, 返回一个天数 (从年份0开始的天数 ).   例: select TO_DAYS(NOW()); +----------------+ | TO_DA ...

  2. [Android Pro] Android应用性能测试之CPU和内存占用(转载)

    首先稍做分析一下测试环境:我们知道CPU和内存占用是一个实时变化的状态,而市面上还没有具体的哪款android应用能做到实时监控CPU和内存占用并使用log日志保存.考虑到android的底层框架是基 ...

  3. 多种非接触卡 ATQA 字节说明

      原文地址 13.56 MHz RFID Software An Open Source implementation of an NFC stack, and various related ut ...

  4. django知识点回顾与补充

    一.django知识点回顾 1.Cookie操作 - 客户端本地存储的键值对 2.Session操作 - 3.URL路由 - /index -> view.函数 4.MTV 5.View中返回方 ...

  5. FishEye

  6. 几款很厉害的jQuery数字化签名插件(转)

    在浏览器中,我们有很多方式来绘制生成签名效果,并且有很多很棒很智能的jQuery插件.数字化签名是未来的发展方向,正是这个原因我们这里收集并且推荐了四款超棒的jQuery数字化签名插件,希望大家喜欢! ...

  7. org.springframework.beans.NotWritablePropertyException

    刚碰到了这个异常,最后发现是bean配置xml文件中的属性名多了一个空格,这就是xml配置spring的弊端啊. 感谢百度,迅速定位了问题. https://yq.aliyun.com/article ...

  8. 最短路径算法(Dijkstra)

    1.建立矩阵,记录任意两点间的直接距离: 2.两个集合,一个集合记录到每个点的最短路径,一个记录前驱节点: 3.主循环,每次找当前点与其他点的距离,记录下最短距离和前驱节点,然后看看通过前驱节点和最短 ...

  9. ios开发-调用系统自带手势

    在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureR ...

  10. Python break

    break退出循环 用 for 循环或者 while 循环时,如果要在循环体内直接退出循环,可以使用 break 语句. 比如计算1至100的整数和,我们用while来实现: sum = 0 x = ...