Intersection of Two Arrays——Array

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2].

class Solution(object):
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        re = []
        n = 0
        for i in range(len(nums1)):
            n |= 1<<nums1[i]
        for i in range(len(nums2)):
            if n&(1<<nums2[i]):
                n &= (~(1<<nums2[i]))
                re.append(nums2[i])
        
        return re

 

猜你喜欢

转载自qu66q.iteye.com/blog/2317215
今日推荐