LintCode:两数组的交

来源:互联网 发布:网络安全事故典型案例 编辑:程序博客网 时间:2024/04/29 16:11

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
0 0
原创粉丝点击