Open zhaokuohaha opened 8 years ago
public class Solution
{
public int[] Intersection(int[] nums1, int[] nums2)
{
SortedSet<int> res = new SortedSet<int>();
foreach(int item in nums1)
{
if (nums2.Contains(item))
res.Add(item);
}
return res.ToArray();
}
}
/**
* [AC] LeetCode 349. Intersection of Two Arrays
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var intersection = function(nums1, nums2) {
var hashTable = {},i;
for(i = 0; i < nums1.length; i++){
hashTable[nums1[i]] = true;
}
var res = [];
for(i = 0; i < nums2.length; i++){
if(hashTable[nums2[i]]){
hashTable[nums2[i]] = false;
res.push(nums2[i]);
}
}
return res;
};
// [AC] 349. Intersection of Two Arrays
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> set = new HashSet(nums1.length);
for(int n : nums1)
set.add(n);
HashSet<Integer> result = new HashSet(nums2.length);
for(int n : nums2)
if(set.contains(n))
result.add(n);
int[] resultToReturn = new int[result.size()];
int index = 0;
for(Integer n : result)
resultToReturn[index++] = n;
return resultToReturn;
}
}
/*
*[AC] LeetCode 349. Intersection of Two Arrays
*/
public class Solution {
int []nums = new int[]{};
public int[] intersection(int[] nums1, int[] nums2) {
if(nums1.length==0 || nums2.length==0)
return nums;
HashSet<Integer> hashSet = new HashSet<Integer>();
for(int i=0;i<nums1.length;i++){
hashSet.add(nums1[i]);
}
List<Integer> list = new ArrayList<Integer>();
for(int i=0;i<nums2.length;i++){
if(hashSet.contains(nums2[i])){
list.add(nums2[i]);
hashSet.remove(nums2[i]);
}
}
nums = new int[list.size()];
for(int i=0;i<list.size();i++){
nums[i]=list.get(i);
}
return nums;
}
}
324. Wiggle Sort II 349. Intersection of Two Arrays