斐波那契数列Python实现
def fbi(n): s1 = 1 s2 = 1 if(n<=0): return -1 else: if( n==1 | n==2): return 1 else: for num in range(2, n): s2 = s1+s2 s1 = s2-s1 return s2 print(fbi(9))
时间复杂度为o(n),空间复杂度为o(1),用的是迭代法想加
def fbi(n): s1 = 1 s2 = 1 if(n<=0): return -1 else: if( n==1 | n==2): return 1 else: for num in range(2, n): s2 = s1+s2 s1 = s2-s1 return s2 print(fbi(9))
时间复杂度为o(n),空间复杂度为o(1),用的是迭代法想加