349. Intersection of Two Arrays --leetcode


编程练习,多多思考。题很简单,我自己总结了下 set 的集合运算见此

题目

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

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

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

python解法

1
2
3
4
5
6
7
8
9
10
class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
num1 = set(nums1)
num2 = set(nums2)
return list(num1 &num2)