Fibonacci
来源:互联网 发布:旺宝免费淘宝收藏软件 编辑:程序博客网 时间:2024/06/18 13:18
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 …
Have you met this question in a real interview? Yes
Example
Given 1, return 0
Given 2, return 1
Given 10, return 34
Note
The Nth fibonacci number won’t exceed the max value of signed 32-bit integer in the test cases.
class Solution { /** * @param n: an integer * @return an integer f(n) */ public int fibonacci(int n) { if (n < 0) return -1; if (n == 1) return 0; if (n == 2) return 1; int fn = 0, fn1 = 1, fn2 = 0; for (int i = 3; i <= n; i++) { fn = fn1 + fn2; fn2 = fn1; fn1 = fn; } return fn; }}
0 0
- Fibonacci
- fibonacci
- Fibonacci
- Fibonacci
- Fibonacci
- fibonacci
- Fibonacci
- Fibonacci
- Fibonacci
- Fibonacci
- Fibonacci
- Fibonacci
- Fibonacci
- Fibonacci
- Fibonacci
- Fibonacci
- Fibonacci
- Fibonacci
- Facebook为虚拟现实视频研发棱锥式编码
- Linux下防火墙开启相关端口及查看已开启端口
- Universal-Image-Loader图片异步加载类库
- Android 6.0 权限问题
- UVA 11624 Fire!(两次bfs)
- Fibonacci
- jdk maven安装配置
- HDU 1026 Ignatius and the Princess I(BFS+记录路径)
- c2a0 这样的空格 NO-BREAK SPACE
- 上传文件到http服务器上
- 分分钟让移动端崩溃重启的代码
- javascript的String对象截取字符串的方法
- Qt下定时器的使用
- 利用pageControl 来做自动轮播