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 answer:
class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
nums = []
for eachnum in nums1:
if eachnum in nums2:
nums2.remove(eachnum)
nums.append(eachnum)
return list(set(nums)) #常用列表去重方法