2017/3/23 15:41:47
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
class Solution(object): def intersection(self, nums1, nums2): return list(set( nums1 ) & set(nums2))
版本1:Java O(m*n) 循环检查
public class Solution { public int[] intersection(int[] nums1, int[] nums2) { Setset = new HashSet (); for ( int i=0;i
版本2:Java O(m+n) 借助哈希表+Set,或者双Set
public int[] intersection(int[] nums1, int[] nums2) { Mapmap = new Hashtable (); Set set = new TreeSet (); for ( int i=0;i