class Solution:
# @param {int[]} nums1 an integer array
# @param {int[]} nums2 an integer array
# @return {int[]} an integer array
def intersection(self, nums1, nums2):
# Write your code here
if nums1 == None or nums2 == None:
return []
nums1 = sorted(nums1)
nums2 = sorted(nums2)
ans = []
i = 0
j = 0
while i < len(nums1) and j < len(nums2):
while i < len(nums1) and j < len(nums2) and nums1[i] < nums2[j]:
i += 1
while i < len(nums1) and j < len(nums2) and nums1[i] > nums2[j]:
j += 1
while i < len(nums1) and j < len(nums2) and nums1[i] == nums2[j]:
ans.append(nums1[i])
i += 1
j += 1
return ans
LintCode:两数组的交
最新推荐文章于 2019-07-02 10:45:01 发布