[LeetCode][Python]349. 兩個數(shù)組的交集
給定兩個數(shù)組,編寫一個函數(shù)來計算它們的交集。
示例 1:
輸入: nums1 = [1,2,2,1], nums2 = [2,2]
輸出: [2]
示例 2:
輸入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
輸出: [9,4]
說明:
輸出結(jié)果中的每個元素一定是唯一的。
我們可以不考慮輸出結(jié)果的順序。
思路:關(guān)于交集第一反應(yīng)是使用Set的交集運算&
看了其他人的思路,基本也是這么運算
#! /usr/bin/env python
#coding=utf-8
class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
return list(set(nums1) & set(nums2))