标签:break space java class htable bre can unique com
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
classSolution(object):
def intersection(self, nums1, nums2):
return list(set( nums1 )& set(nums2))
publicclassSolution{
publicint[] intersection(int[] nums1,int[] nums2){
Set<Integer> set =newHashSet<Integer>();
for(int i=0;i<nums1.length;i++)
for(int j=0;j<nums2.length;j++)
if( nums1[i]== nums2[j]){
set.add(nums1[i]);
break;
}
Object[] obj = set.toArray();
int[] rs =newint[obj.length];
for(int i=0;i<obj.length;i++)
rs[i]=(int)obj[i];
return rs;
}
}
publicint[] intersection(int[] nums1,int[] nums2){
Map<Integer,Boolean> map =newHashtable<Integer,Boolean>();
Set<Integer> set =newTreeSet<Integer>();
for(int i=0;i<nums1.length;i++)
map.put( nums1[i],true);
for(int j=0;j<nums2.length;j++){
if(map.get(nums2[j])==null)continue;
set.add(nums2[j]);
}
int i =0;
int[] rs =newint[set.size()];
for(Integer num : set )
rs[i++]= num;
return rs;
}
349. Intersection of Two Arrays【双指针|二分】
标签:break space java class htable bre can unique com
原文地址:http://www.cnblogs.com/flyfatty/p/6624806.html