python 基础编程之求两个数组的交集

python 基础编程

给定两个有序数组,求这两个数组的交集,要求时间复杂度O(m+n)

代码如下:

#coding=utf-8
list_a = [1,3,4,5,7,9]
list_b = [2,3,5,8,9]
list_commen = []
pointer_a = 0
pointer_b = 0
while pointer_a < len(list_a) and pointer_b < len(list_b):
    if list_a[pointer_a] < list_b[pointer_b]:
        pointer_a += 1
    elif list_a[pointer_a] > list_b[pointer_b]:
        pointer_b += 1
    else:
        list_commen.append(list_a[pointer_a])
        pointer_a += 1
        pointer_b += 1

print list_commen

你可能感兴趣的:(python基础编程)