欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Intersection of Two Arrays——Array

程序员文章站 2024-02-29 17:43:34
...

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

 

上一篇: 字符串---删除子串(1)

下一篇: