把两个有序的列表合为一个有序的列表,它的时间复杂度为O(n)
def hb(list1,list2):
result = []
while list1 and list2:
if list1[0] < list2[0]:
result.append(list1[0])
del list1[0]
else:
result.append(list2[0])
del list2[0]
if list1:
result.extend(list1)
if list2:
result.extend(list2)
print(result)
return result
list1 = [3,4,7,9,11]
list2 = [1,2,5,8,13,20]
hb(list1, list2)
>>> [1, 2, 3, 4, 5, 7, 8, 9, 11, 13, 20]
---------------------
作者:C__R
来源:CSDN
原文:https://blog.csdn.net/weixin_41667438/article/details/79181032
版权声明:本文为博主原创文章,转载请附上博文链接!