leetCode Intersection of two arrays II problem record

Questions are as follows:

Given two arrays, write a method to compute their intersection.

E.g:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2 ].

Notice:

   The number of occurrences of each element in the output should be the same as the number of occurrences of the element in the two arrays.
   We can ignore the order of output results.
follow up:

What if the given array is already sorted? How would you optimize your algorithm?
If the size of nums1 is much smaller than nums2, which method is better?
If the elements of nums2 are stored on disk and memory is limited, you can't load all the elements into memory at once, what should you do?

 

own solution:

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
         HashMap<Integer,Integer> hashMap = new HashMap<>();
        ArrayList<Integer> result = new ArrayList<>();

        if (nums1.length > 0){
            for (int i =0;i<=nums1.length-1;i++){
                Integer temp = hashMap.get(nums1[i]);
                if (temp == null){
                    hashMap.put(nums1[i],1);
                }else {
                    hashMap.put(nums1[i],temp+1);
                }
                
            }
            for(int j=0;j<=nums2.length-1;j++){
                Integer temp = hashMap.get(nums2[j]);

                if (temp != null){
                    result.add(nums2[j]);

                    if (temp> 1){
                        hashMap.put(nums2[j],temp-1);
                    }else {
                        hashMap.remove(nums2[j]);
                    }
                }
            }
        }

        Integer[] integers = new Integer[result.size()];
        result.toArray(integers);
        int tmpInt[] = new int[result.size()];

        for (int i = 0; i < integers.length; i++) {
            tmpInt[i] = integers[i];
        }

        return tmpInt;
    }
}

 

Ideas: 1. Traverse array 1 first, and store each item of the array in a hashMap. If an element appears multiple times, the value of the hashmap+1

          2. Traverse array 2 to determine whether the current element exists in the hashmap, and if so, add this element to the result

          3. Determine the value of the current element, if it is greater than 1, subtract 1, otherwise remove the key of this element from the hash,

          4. The final result is the repeated part of the two arrays required

 

The optimal solution given by leet

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        int len1 = nums1.length;
        int len2 = nums2.length;
        int len = len1 > len2 ? len2 : len1;

        Arrays.sort(nums1);
        Arrays.sort(nums2);

        int[] nums = new int[len];
        int k = 0;
        int curr1, curr2 = 0;
        for(int i = 0, j = 0; i < len1 && j < len2;) {
            curr1 = nums1[i];
            curr2 = nums2[j];
            if(curr1 == curr2) {
                nums[k] = curr1;
                k += 1;
                i += 1;
                j += 1;
            } else if(curr1 < curr2) {
                i += 1;
            } else {
                j += 1;
            }
        }

        return Arrays.copyOfRange(nums, 0, k);     
    }
}

 

Try to understand the idea:

  1. Find which of two arrays is relatively shorter

  2. Sort two arrays

  3. Traverse the two arrays from 0, and judge whether the elements of array 1 and array 2 currently traversed are equal. If they are equal, it means that the repeated part of the two arrays begins. Put this element in the returned array at the position where the indicator k is located, and then k+1

  4. After traversing and playing two arrays, intercept the returned array to get the array of repeated parts

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326358030&siteId=291194637