`

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

 

分享到:
评论

相关推荐

    leetcode分类-interview:面试基础算法

    intersection of two arrays 350: intersection of two arrays ii medium 33: search in sorted array 81: search in rotated sorted array ii 153: find minimum in rotated sorted array 162: find peak element ...

    leetcode530-alogritme-interview:alogritme-面试

    Array is Sorted 167 125 344 345 11 1-5 滑动窗口 Minimum Size Subarray Sum 209 3 438 76 第二章 查找表相关问题 2-1 set的使用 Intersection of Two Arrays 349 2-2 map的使用 Intersection of Two Arrays II ...

    戳气球leetcode-leetcode:leetcode

    Intersection of Two Arrays 17.10. Find Majority Element LCCI Game of Life Find All Numbers Disappeared in an Array Shortest Unsorted Continuous Subarray Rotate Image 宝石与石头Jewels and Stones Kids ...

    leetcode中国-DP:DP

    leetcode中国大批 ...Intersection of the two sorted arrays. 7. Write a program to cyclically rotate an array by one. 8. find Largest sum contiguous Subarray [V. IMP] 9* Minimise the maximum

    leetcode中国-Final450_Data-Structures:Final450_数据结构

    leetcode中国Final450_数据结构 实际上,这个存储库包含...Intersection of the two sorted arrays. *Write a program to cyclically rotate an array by one. *find Largest sum contiguous Subarray [V. IMP] *Minimi

    JAVA程序设计上机课4_阳甫军‘.pptx

    2. LeetCode 349:Intersection of Two Arrays(两数组的交集) - 要求找到两个整数数组的交集,返回的结果应无重复。同样需要提交代码及运行结果截图。 通过完成这些练习,学生可以进一步巩固数组操作、链表处理...

    leetcode答案-leetcode:leetcode.com的答案

    而C++的模板和STL容器在处理"数组的交集"(Intersection of Two Arrays)时,能提供高效的解决方案。 五、源码解析 压缩包中的"leetcode-master"很可能是一个包含LeetCode解题源码的项目。通过阅读和分析这些源码...

    JavaScript获取两个数组交集的方法

    /* finds the intersection of * two arrays in a simple fashion. * * PARAMS * a - first array, must already be sorted * b - second array, must already be sorted * * NOTES * * Should have O(n) ...

    LeetCode最全代码

    421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...

    LeetCode:LeetCode刷题

    例如,"两个数组的交集"(Intersection of Two Arrays)问题,找出两个数组的交集元素。 10. **堆(Heap)**:最大堆和最小堆可用于找到数组中的最大值或最小值,或者用于优先队列。例如,"堆排序"(Heap Sort)...

    i-vector的工具箱

    A speaker recognition system includes two primary components: a front-end and a back-end. The front-end transforms acoustic waveforms into more compact and less redundant representations called ...

    UG6.0快捷键大全

    MESSAGE Modifies the faces of a body by the use of various methods. HINT This command is superseded by Synchronous Modeling commands. To run this legacy command, set UGII_DMX_NX502=1. HELP DSN_face...

Global site tag (gtag.js) - Google Analytics