当前位置:网站首页>剑指Offer(七):斐波那契数列
剑指Offer(七):斐波那契数列
2022-07-26 10:35:00 【康斯坦奇】
解法1:这道题递归很好写,但是存在很严重的效率问题。
class Solution:
def Fibonacci(self, n):
# write code here
if n == 0:
return 0
if n == 1:
return 1
return self.Fibonacci(n-2)+self.Fibonacci(n-1)
解法2:循环
class Solution:
def Fibonacci(self, n):
# write code here
if n <= 1:
return n
a = 0
b = 1
c = 0
for i in range(2,n+1):
c = a + b
a = b
b = c
return c
边栏推荐
猜你喜欢
随机推荐
Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
【机器学习小记】【搭建循环神经网络及其应用】deeplearning.ai course5 1st week programming(keras)
[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间
[转]ArcGIS中判断两个Geometry之间的关系
STM32 Alibaba cloud mqtt esp8266 at command
Introduction to Phoenix (Level 1: Phoenix installation, level 2: Phoenix basic grammar)
Redis docker instance and data structure
mysql 进不去了怎么办
Controller返回JSON数据
Issue 7: how do you choose between curling up and lying flat
The problem of large fluctuation of hx711 data
卸载魅族应用商店
[Halcon vision] image filtering
第4期:大学生提前职业技能准备之一
.NET操作Redis sorted set有序集合
[Halcon vision] polar coordinate transformation
图片随手机水平移动-陀螺仪。360度设置条件
英语基础句型结构------起源
JS对象赋值问题
多目标优化系列1---NSGA2的非支配排序函数的讲解