时间复杂度:
空间复杂度:
时间比空间重要
递归:
递归特征:
递归案例:
汉诺塔问题:
def hanoi(n,A,B,C):
if n>0:
hanoi(n-1,A,C,B)
print("moving from %s to %s"%(A,C))
hanoi(n-1,B,A,C)
hanoi(3,"a","b","c")
moving from a to c
moving from a to b
moving from c to b
moving from a to c
moving from b to a
moving from b to c
moving from a to c
汉诺塔移动次数的递推式:h(x)=2h(x)+1
原视频:https://www.bilibili.com/video/BV1uA411N7c5?p=7&spm_id_from=pageDriver&vd_source=9baef983d7bc08245d4dee5c9e676ee9