The intersection of two arrays of LeetCode data structure and algorithm II

Get into the habit of writing together! This is the 12th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

topic

350. Intersection of Two Arrays II

nums1Given the sum of two integer arrays  nums2, please return the intersection of the two arrays as an array. The number of occurrences of each element in the returned result should be the same as the number of times the element appears in both arrays (if the number of occurrences is inconsistent, the smaller value is considered). The order of output results can be ignored.

Example 1:

输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2,2]
复制代码

Example 2:

输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[4,9]
复制代码

hint:

1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
复制代码

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

answer

problem-solving analysis

Problem solving ideas

  1. Idea: sort two arrays, and then use double pointers to get the intersection of the two arrays
  2. Sort first, then iterate over both arrays with double pointers
  3. Initially, the two pointers point to the head of the array respectively. Compare two numbers in two pointers at a time:
    • If the two numbers are not equal, the pointer to the smaller number is shifted right by one,
    • If the two numbers are equal, add that number to the answer, and move both pointers to the right by one.
    • The traversal ends when at least one pointer exceeds the bounds of the array.

the complexity

Time complexity O(N)
Space complexity O(|Σ|)

problem solving code

The solution code is as follows (detailed comments in the code):

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        // 1. 排序
        Arrays.sort(nums1);
        Arrays.sort(nums2);

        int length1 = nums1.length, length2 = nums2.length;
        int[] intersection = new int[Math.min(length1, length2)];
        int index1 = 0, index2 = 0, index = 0;
        // 2. 两个指针 index1, index2
        while(index1 < length1 && index2 < length2) {
            if (nums1[index1] < nums2[index2]) {
                index1++;
            } else if (nums1[index1] > nums2[index2]) {
                index2++;   
            } else {
                intersection[index] = nums1[index1];
                index1++;
                index2++;
                index++;
            }
        }
        return Arrays.copyOfRange(intersection, 0, index);
    }
}
复制代码

Feedback results after submission (because this topic has not been optimized, the performance is average):

image.png

Reference Information

Guess you like

Origin juejin.im/post/7085707726776434695