LeetCode-349&350.Intersection of Two Arrays

349 https://leetcode.com/problems/intersection-of-two-arrays/

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

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

Note:

  • Each element in the result must be unique.
  • The result can be in any order.

这是一道有陷阱的简单题

方法1

代码用了两个set存放原始num,为的是去掉重复元素。如果不适用set2,会存在重复元素的问题

vector<int> intersection(vector<int>& nums1, vector<int>& nums2) 
{
	unordered_set<int> set1,set2;
	for (int n : nums1)
		set1.insert(n);
	for (int n : nums2)
		set2.insert(n);
	vector<int> res;
	for (int n : set2)
	{
		if (!set1.insert(n).second)
		    res.push_back(n);
	}
	return res;
}

方法2

排序后使用双指针

vector<int> intersection(vector<int>& nums1, vector<int>& nums2) 
    {
        int n1 = nums1.size(), n2 = nums2.size(),i=0,j=0;
    	vector<int> res;
    	if (n1 == 0 || n2 == 0)
    		return res;
    	sort(nums1.begin(), nums1.end());
    	sort(nums2.begin(), nums2.end());
    	while (i<n1&&j<n2)
    	{
    		if (nums1[i] < nums2[j])
    			i++;
    		else if (nums1[i] > nums2[j])
    			j++;
    		else
    		{
    			if (find(res.begin(), res.end(), nums1[i]) == res.end())
    				res.push_back(nums1[i]);
    			i++;
    			j++;
    		}
    	}
    	return res;
    }


二分查找

bool search(vector<int> nums, int n)
    {
    	int l = 0, r = nums.size() - 1, m;
    	while (l <= r)
    	{
    		m = (l + r) / 2;
    		if (nums[m] < n)
    			l = m + 1;
    		else if (nums[m] > n)
    			r = m - 1;
    		else
    			return true;
    	}
    	return false;
    }
    
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) 
    {
        int n1 = nums1.size(), n2 = nums2.size();
    	vector<int> res;
    	if (n1 == 0 || n2 == 0)
    		return res;
    	sort(nums1.begin(), nums1.end());
    	for (int n : nums2)
    	{
    		if (search(nums1,n)&& find(res.begin(), res.end(), n) == res.end())
    			res.push_back(n);
    	}
    	return res;
    }


350 https://leetcode.com/problems/intersection-of-two-arrays-ii/

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

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

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?

方法1

直接在nums1中查找每一个nums2元素,如果存在就加入到结果列表,并在nums1中删除当前元素

vector<int> intersect(vector<int>& nums1, vector<int>& nums2) 
    {
        int n1 = nums1.size(), n2 = nums2.size();
    	vector<int> res;
    	if (n1 == 0 || n2 == 0)
    		return res;
    	for (int n : nums2)
    	{
    		auto i = find(nums1.begin(), nums1.end(), n);
    		if ( i != nums1.end())
    		{
    			res.push_back(n);
    			nums1.erase(i);
    		}
    	}
    	return res;
    }

方法2

同349一样用双指针,同时不用判断是否存在在结果列表中

vector<int> intersect(vector<int>& nums1, vector<int>& nums2) 
    {
        int n1 = nums1.size(), n2 = nums2.size(), i = 0, j = 0;
    	vector<int> res;
    	if (n1 == 0 || n2 == 0)
    		return res;
    	sort(nums1.begin(), nums1.end());
    	sort(nums2.begin(), nums2.end());
    	while (i < n1&&j < n2)
    	{
    		if (nums1[i] < nums2[j])
    			i++;
    		else if (nums1[i] > nums2[j])
    			j++;
    		else
    		{
    			res.push_back(nums1[i]);
    			i++;
    			j++;
    		}
    	}
    	return res;
    }

方法3

计数法 空间消耗O(m),如果不判断是否存在,则消耗O(m + n)。说明参考 https://leetcode.com/discuss/103787/table-solution-pointers-solution-with-time-space-complexity 

Based on C++ map mechanism, if a key is not exist, access the key will assign a default value to the key. so if you simply test if map[key] is 0 or not by using "if (map[key] == 0)" without testing if the key is in the map. you will consume extra space....you could avoid allocate extra space either by find or count method. I usually use count, it is more concise. .

vector<int> intersect(vector<int>& nums1, vector<int>& nums2) 
    {
        int n1 = nums1.size(), n2 = nums2.size();
    	vector<int> res;
    	if (n1 == 0 || n2 == 0)
    		return res;
    	unordered_map<int, int> dict;
    	for (int n : nums1) 
    		dict[n]++;
    	for (int n : nums2)
    		if (dict.find(n) != dict.end() && --dict[n] >= 0)
    			res.push_back(n);
    	return res;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值