366. Fibonacci【Naive】
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.
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】的更多相关文章
- 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 ...
- 452. Remove Linked List Elements【Naive】
Remove all elements from a linked list of integers that have value val. Example Given 1->2->3- ...
- 466. Count Linked List Nodes【Naive】
Count how many nodes in a linked list. Example Given 1->3->5, return 3. 解法一: /** * Definition ...
- 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 / \ ...
- 【HDU1848】Fibonacci again and again(博弈论)
[HDU1848]Fibonacci again and again(博弈论) 题面 Hdu 你有三堆石子,每堆石子的个数是\(n,m,p\),你每次可以从一堆石子中取走斐波那契数列中一个元素等数量的 ...
- 【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 ...
- 【HDU3117】Fibonacci Numbers
[HDU3117]Fibonacci Numbers 题面 求斐波那契数列的第\(n\)项的前四位及后四位. 其中\(0\leq n<2^{32}\) 题解 前置知识:线性常系数齐次递推 其实后 ...
- HDU 1568 Fibonacci【求斐波那契数的前4位/递推式】
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- POJ 3070 Fibonacci【斐波那契数列/矩阵快速幂】
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17171 Accepted: 11999 Descr ...
随机推荐
- [Android Studio] Android Studio快速定位当前打开的文件在哪个目录(package)下
转载自:http://blog.csdn.net/hyr83960944/article/details/38067499 在Eclipse中有一个很好的功能,就是比如我打开一个AActivity,左 ...
- C++内存管理学习堆和栈
来源:http://c.chinaitlab.com/basic/936306_2.html 一 C++内存管理 1.内存分配方式 在讲解内存分配之前,首先,要了解程序在内存中都有什么区域,然后再详细 ...
- DRP经验总结
思想 指导 从开始看DRP项目到完成已经有三个月左右的时间了,这是一个足够长的视频,当看第一集的时候就再想,啥时候看完呢? 其间,也断断续续,有时看的效率高有时相反,有时几天看不了几集,好在总算看完了 ...
- Linux文件系统---用户与内核的交互接口
从磁盘到数据,从数据到文件,从文件到目录,从目录到文件系统,从文件系统到操作系统.构成了计算机中的IO读写机制. 整个磁盘可以分为1个MBR(Master Boot Record)和4个partiti ...
- unity 统一替换shader
camera.SetReplacementShader(shader,"tag"); 可以切了测试性能
- jQuery几个经典表单应用整理回想
1.文本框获得(失去)焦点 当文本框获得输入焦点时,将该文本框高亮显示,算不得一个应用.仅仅是一个小技巧,能够提高用户体验. [html] view plaincopy <span style= ...
- [置顶] ArcGIS Runtime SDKs 10.2 for iOS & Android& OS X发布
我们高兴的宣布:ArcGISRuntime SDKs 10.2 for iOS & Android & OS X正式发布!在10.2版本中,你可以在iOS.Android和Mac设备上 ...
- Python 遍历dict
遍历dict 由于dict也是一个集合,所以,遍历dict和遍历list类似,都可以通过 for 循环实现. 直接使用for循环可以遍历 dict 的 key: >>> d = { ...
- java web 中的MVC
M:相当于Bean V:jsp C:servlet 当客户端发来请求,servlet响应请求处理请求,并把要发送给客户端的数据封装到Bean中,然后通过转发,将这个封装好了数据Bean送给jsp,js ...
- 【APP接口开发】chrome浏览器DHC工具安装使用(亲测有效)
1.DHC文件获取地址:http://chromecj.com/web-development/2015-08/549/download.html 2.chrome安装DHC插件教程和步骤:http: ...