LintCode:两数组的交

LintCode:两数组的交

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,python)