斐波拉切三种求法

# 暴力求解
def func1(num: int):
if num < 2:
return num
return func1(num - 1) + func1(num - 2)


# 去重队列


def recurse(li: list, num: int):
if num == 0:
return 0
if num == 1:
return 1
if li[num] != 0:
return li[num]
li[num] = recurse(li, num - 1) + recurse(li, num - 2)
return li[num]


def func2(num: int):
li = [0 for _ in range(num + 1)]
return recurse(li, num)

# 双指针
def func3(num:int):
pre1 = 0
pre2 = 1
res = 0
for i in range(0,num-1): # 少循环2次:0,1
res = pre1 + pre2
pre1 = pre2
pre2 = res
return res


print(func1(3))
print(func2(3))
print(func3(3))
posted @ 2022-05-14 11:48  狒狒桑  阅读(44)  评论(0编辑  收藏  举报