SHU-2016-SummerPractice / AlgorithmExerciseIssues

算法训练相关文件及工单。
https://github.com/SHU-2016-SummerPractice/AlgorithmExerciseIssues/issues
2 stars 0 forks source link

排序 - 2016-09-07 #50

Open zhaokuohaha opened 7 years ago

zhaokuohaha commented 7 years ago

324. Wiggle Sort II 349. Intersection of Two Arrays

zhaokuohaha commented 7 years ago

349 - C# - 这也能过

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();
    }
}
dayang commented 7 years ago
/**
 * [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;
};
wolfogre commented 7 years ago
// [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;
    }
}
SnackMen commented 7 years ago
/*
*[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;
    }
}