当前位置:网站首页>LeetCode刷题——泰波那契数列
LeetCode刷题——泰波那契数列
2022-06-29 08:41:00 【书香恋仁心】
泰波那契数列:n=0,T(0)=0 n=1,T(1)=1 n=2,T(2)=1
n>=3时,T(n) =T(n-1)+T(n-2)+T(n-3)
解题思路
n>2时,设了a0,a1,a2=0,1,1
假设n=3,则t_3 = a0+a1+a2 其实也就是前三项和
如何再将a1赋值给a0,a2赋值给a1,前三项和赋值给a2(ao=a1,a1=a2,a2=t_3)
循环次数就是
假设
n=3 n-2=1
n=4 n-2=2
n=5 n-2=3
class Solution:
def tribonacci(self, n):
if n == 0:
return 0
if n <= 2:
return 1
a0, a1, a2 = 0, 1, 1
for i in range(n -2):
num = a0 + a1 + a2
a0 = a1
a1 = a2
a2 = num
return num
n>=3时,本来想用递归解决的,但是超过了python的最大递归深度
递归代码如下
import sys
sys.setrecursionlimit(1000)
class Solution:
def tribo_nacci(self, n):
if n == 0:
return 0
elif n <= 2:
return 1
else:
return self.tribo_nacci(n - 1) + self.tribo_nacci(n - 2) + self.tribo_nacci(n - 3)
s = Solution()但是非常不建议用递归来写,因为他真的非常消耗cpu资源
边栏推荐
- Wechat applet project: tab navigation bar
- 两阶段目标检测原理详细版
- The difference between cokkie and session
- (转)MySQL: ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
- 《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(五)
- Simple use of promise method
- Universal target detection based on region attention
- 1.4 regression of machine learning methods
- YOLO Nano:一种高度紧凑的只看一次的卷积神经网络用于目标检测
- Uber 前安全主管面临欺诈指控,曾隐瞒数据泄露事件
猜你喜欢

Open3d farthest point sampling (FPS)
![[target detection] | indicator a probabilistic challenge for object detection](/img/82/7b830d44bed7b1509cb0cdfed88e3e.png)
[target detection] | indicator a probabilistic challenge for object detection

keras转tf.keras中VGG19 input_shape
![[to.Net] C data model, from Entity Framework core to LINQ](/img/98/6a8b295d1465697945e01b8c48ec52.png)
[to.Net] C data model, from Entity Framework core to LINQ

Detailed version of two-stage target detection principle

商业智能BI的未来,如何看待AI+BI这种模式?

Wechat applet user refuses to authorize geographic location information and calls up the authorization window again

1.4 regression of machine learning methods

Handwritten virtualdom

Pytorch Summary - Automatic gradient
随机推荐
Wechat applet sub components transfer values to the page (communication between parent and child components) with source code
Open3d hidden point removal
Mh/t 6040 smoke density test of aviation materials
深入RC、RS、DaemonSet、StatefulSet(七)
CMD enter virtual machine
数据处理时代,数据质量建设才是企业的生存之道
Modify EXIF information
GPU training cloud platform record
Chapter 12 signals (II) - examples of producers and consumers
After aikuai is installed or reset, the PC or mobile terminal cannot obtain IP
SSD Improvement cfenet
Augfpn: improved multiscale feature learning for target detection
网络安全问题
Simple use of promise method
Ue4 installe le plug - in datasmith dans la version 4.20-23
GPU训练云平台记录
超融合架构和传统架构有什么区别?
Pytorch summary learning series - operation
UE4 在viewport视口中显示3D可编辑点
UE4 display 3D editable points in Viewport