Given two arrays, write a function to compute their intersection.python
Example 1:git
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Example 2:github
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.函數
給定兩個數組,編寫一個函數來計算它們的交集。ui
示例 1:code
輸入: nums1 = [1,2,2,1], nums2 = [2,2]
輸出: [2]
示例 2:ip
輸入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
輸出: [9,4]
說明:utf-8
輸出結果中的每一個元素必定是惟一的。
咱們能夠不考慮輸出結果的順序。element
# -*- coding: utf-8 -*- # @Author: 何睿 # @Create Date: 2019-04-09 16:11:29 # @Last Modified by: 何睿 # @Last Modified time: 2019-04-09 16:15:56 class Solution: def intersection(self, nums1: [int], nums2: [int]) -> [int]: # python 內置集合運算 return list(set(nums1) & set(nums2))