leetcode350. Intersection of Two Arrays II

nums1Given the sum of two integer arrays nums2, please return the intersection of the 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:
 Input: nums1 = [1, 2, 2, 1], nums2 = [2, 2]
 Output: [2, 2]

Idea 1: Sort + double pointers
We can sort the two arrays given first, and then use double pointers to traverse the two ordered sequences. The traversal process is as follows:

  1. If the elements pointed to by the two pointers are not equal, the smaller pointer goes backward.
  2. If the elements pointed to by the two pointers are equal, the element belongs to the intersection, and the two pointers move backward at the same time.
  3. If one of the sequences is finished, the traversal ends.

Animated demo:
insert image description here
Code 1:

//排序+双指针
class Solution {
    
    
public:
	vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
    
    
		//1、将nums1和nums2分别进行排序
		sort(nums1.begin(), nums1.end());
		sort(nums2.begin(), nums2.end());
		//2、使用双指针找出num1和nums2的交集
		vector<int> vRet;
		int len1 = nums1.size(), len2 = nums2.size();
		int pos1 = 0, pos2 = 0;
		while (pos1 < len1&&pos2 < len2)
		{
    
    
			if (nums1[pos1] < nums2[pos2]) //两个指针指向的元素不相等
			{
    
    
				pos1++; //小的往后走
			}
			else if (nums2[pos2] < nums1[pos1]) //两个指针指向的元素不相等
			{
    
    
				pos2++; //小的往后走
			}
			else //两个指针指向的元素相等
			{
    
    
				vRet.push_back(nums1[pos1]); //该元素属于交集
				//两个指针一起往后走
				pos1++;
				pos2++;
			}
		}
		return vRet;
	}
};

Idea 2: Hash table
For this problem, we can use a hash table to solve the problem. The steps are divided into two steps:

  1. Put the array with fewer elements into the hash table first, and count the number of occurrences corresponding to each element.
  2. Iterate over another array to find the intersection of the two arrays.

When traversing another array and using a hash table to find the intersection of two arrays, the specific search method is as follows:

  1. If the traversed element appears in the hash table, the element belongs to the intersection, and the number of occurrences of the element in the hash table is reduced by one, and if the number of occurrences of the element in the hash table is reduced to 0, the element is Remove from hash table.
  2. If the traversed element does not appear in the hash table, continue to traverse the next element.

This is actually to record the elements of an array with a hash table first, then traverse the elements of another array, compare the data in the hash table to find the elements that can be offset, and the elements that can be offset are the two arrays. intersection.

Animated demonstration:
insert image description here
Explain: choosing to put an array with a smaller number of elements into the hash table can reduce the space complexity.

Code two:

//哈希表
class Solution {
    
    
public:
	vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
    
    
		//为了降低空间复杂度,应该先将元素个数较少的数组放入哈希表
		if (nums1.size() > nums2.size())
		{
    
    
			return intersect(nums2, nums1);
		}
		//1、将nums1中的每个数字以及对应出现的次数放入哈希表中
		unordered_map<int, int> um;
		for (auto e : nums1)
		{
    
    
			um[e]++;
		}
		//2、遍历nums2,找出nums1和nums2的交集
		vector<int> vRet;
		for (auto e : nums2)
		{
    
    
			if (um.count(e)) //该元素在哈希表中
			{
    
    
				vRet.push_back(e); //该元素属于交集
				um[e]--; //减少该元素在哈希表中出现的次数
				if (um[e] == 0) //该元素的次数已经变为0了
				{
    
    
					um.erase(e); //将该元素从哈希表中删除
				}
			}
		}
		return vRet;
	}
};

Guess you like

Origin blog.csdn.net/chenlong_cxy/article/details/122343093