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.
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))
class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ rlist=[] i=j=0 nums1.sort() nums2.sort() n=len(nums1) m=len(nums2) while i<n and j<m: if nums1[i]<nums2[j]: i+=1 elif nums1[i]>nums2[j]: j+=1 else: if len(rlist)==0 or rlist[-1]!=nums1[i]: rlist.append(nums1[i]) i+=1 j+=1 return rlist