Intersection of Two Arrays

Given two arrays, write a function to compute their intersection.git

Example 1:github

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]

Example 2:app

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]

Note:code

  • Each element in the result must be unique.
  • The result can be in any order.
public static  int[] intersection(int[] nums1, int[] nums2) {
    Set<Integer>  set = new HashSet<>();
    List<Integer> list = Arrays.stream(nums1).map(x->x).boxed().collect(Collectors.toList());
    for (int i = 0;i< nums2.length;i++){
        if(list.contains(nums2[i])){
            set.add(nums2[i]);
        }
    }

    int[] d = new int[set.size()];
    int i =0;
    for (int num : set) {
        d[i] = num;
        i++;
    }
    return d;
}

350. Intersection of Two Arrays IIelement

Easyleetcode

528182FavoriteShareget

Given two arrays, write a function to compute their intersection.it

Example 1:io

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Example 2:function

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1's size is small compared to nums2's size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
//這種使用map 通常均可以解決  可能效率沒有那麼快
public static int[] intersect(int[] nums1, int[] nums2) {

    Map<Integer, Integer> map = new HashMap<>();
    Map<Integer, Integer> map2 = new HashMap<>();
    //
    List<Integer> list = new ArrayList<>();
    for (int i = 0; i < nums1.length; i++) {
        if (map.containsKey(nums1[i])) {
            map.put(nums1[i], map.get(nums1[i]) + 1);
        }else{
            map.put(nums1[i], 1);
        }
    }
    for (int j = 0; j < nums2.length; j++) {
        if (map2.containsKey(nums2[j])) {
            map2.put(nums2[j], map2.get(nums2[j]) + 1);
        }else{
            map2.put(nums2[j], 1);
        }
    }
    for (Integer key : map.keySet()) {

        int length1= map.get(key);
        int length2=map2.get(key) == null?0:map2.get(key);
        int kl = length1> length2?length2:length1;

        for(int k = 0;k<kl;k++){
            list.add(key);
        }


    }
     int d[] = new int[list.size()];
     for (int m =0;m<list.size();m++){
          d[m] = list.get(m);
     }
     return d;

}

//相似的 只是簡化一下
public int[] intersect2(int[] nums1, int[] nums2) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    ArrayList<Integer> result = new ArrayList<Integer>();
    for(int i = 0; i < nums1.length; i++)
    {
        if(map.containsKey(nums1[i])) {
            map.put(nums1[i], map.get(nums1[i])+1);
        } else {
            map.put(nums1[i], 1);
        }
    }

    for(int i = 0; i < nums2.length; i++)
    {
        if(map.containsKey(nums2[i]) && map.get(nums2[i]) > 0)
        {
            result.add(nums2[i]);
            map.put(nums2[i], map.get(nums2[i])-1);
        }
    }

    int[] r = new int[result.size()];
    for(int i = 0; i < result.size(); i++)
    {
        r[i] = result.get(i);
    }

    return r;
}

git:https://github.com/woshiyexinjie/leetcode-xin

相關文章
相關標籤/搜索