《剑指offer》1: 斐波那契数列
2020-08-03 21:01
976 查看
题目描述
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0,第1项是1)。n<=39
这个题目大家在学编程的时候都应该遇到过,但是不能够使用递归解法,因为如果使用递归,就会超出时间,算法的复杂度是2^n。因此这里采用迭代的解法。
代码如下所示:
# -*- coding:utf-8 -*- class Solution: def Fibonacci(self, n): if n==1: return 1 if n==0: return 0 a=1 b=0 ret=0 for i in range(0,n-1): ret=a+b b=a a=ret return ret
相关文章推荐
- 面试题09_斐波那契数列及其变种详解——剑指offer系列
- 剑指offer_面试题9_斐波那契数列
- 《剑指Offer》读书笔记07:斐波那契数列
- 剑指offer-chapter2-面试题9-斐波那契数列(java)
- 《剑指offer》编程题java实现(六):斐波那契数列系列问题
- 剑指Offer----面试题九:斐波那契数列
- 剑指offer-斐波那契数列
- 剑指offer:面试题9,斐波那契数列
- 《剑指offer》之斐波那契数列
- 剑指offer(C++)——斐波那契数列
- JAVA实现斐波那契数列问题(《剑指offer》)
- 剑指offer—斐波那契数列
- 剑指offer_09 斐波那契数列
- 剑指offer:面试题9,斐波那契数列
- 剑指Offer——斐波那契数列
- 剑指offer-斐波那契数列
- 牛客网-《剑指offer》-斐波那契数列
- 剑指offer 斐波那契数列
- 《剑指offer》面试题9:斐波那契数列
- 重做剑指offer(七)——斐波那契数列