Skip to content

fib数列 #28

@zhaochj

Description

@zhaochj

def fib1(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib1(n-1) + fib1(n-2)

memo = {0:0, 1:1}
def fib2(n):
if not n in memo:
memo[n] = fib2(n-1)+fib2(n-2)
return memo[n]

第二种方法似乎不会在计算上有什么优势,求大于1的fib数列都需要全部计算一次。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions