算法:
代码:
L1 = [13, 57, 61, 114, 987, 1000]
L2 = [5, 23, 57, 63, 114, 257, 1000]
L = []
i = j = 0
while i < len(p1) and j < len(p2):
if L1[i] == L2[j]:
L.append(L1[i])
i += 1
j += 1
elif L1[i] > L2[j]:
j += 1
else:
i += 1
L
[57, 114, 1000]
代码2:
def Intersection(p1, p2):
answer = []
i = j = 0
while i < len(L1) and j < len(L2):
if p1[i] == p2[j]:
answer.append(p1[i])
i += 1
j += 1
elif p1[i] > p2[j]:
j += 1
else:
i += 1
return answer
Intersection([13, 57, 61, 114, 987, 1000], [5, 23, 57, 63, 114, 257, 1000])
[57, 114, 1000]
转载请注明原文地址:https://blackberry.8miu.com/read-14251.html