lc349. Intersection of Two Arrays

264 阅读1分钟

349. Intersection of Two Arrays

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

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2] Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [9,4] Note:

Each element in the result must be unique. The result can be in any order.

思路:不要求顺序,结果中每个元素唯一,用集合set来做,求set的交集

代码:python3

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        return list(set(nums1)&set(nums2))