LeetCode(350)Intersection of Two Arrays II

原创 2016年08月29日 21:21:17

题目

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?

分析

求含重复元素的两数组交集,同样采用哈希的思想,求交集同时更新元素个数。

代码

#include <iostream>
#include <cstdlib>
#include <vector>
#include <map>

using namespace std;

class Solution {
public:
	vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
		if (nums1.empty() || nums2.empty())
			return vector<int>();

		int l1 = nums1.size(), l2 = nums2.size();
		map<int, int> m;
		for (int i = 0; i < l1; ++i)
		{
			++m[nums1[i]];
		}//for

		vector<int> ret;
		for (int i = 0; i < l2; ++i)
		{
			if (m[nums2[i]] > 0)
			{
				ret.push_back(nums2[i]);
				--m[nums2[i]];
			}//if
		}//for

		return ret;
	}
};

int main()
{
	vector<int> v1 = { 1,2,2,1 }, v2 = { 2,2 };
	vector<int> ret = Solution().intersect(v1, v2);

	system("pause");
	return 0;
}
GitHub源码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fly_yr/article/details/52356641

leetcode_350 Intersection of Two Arrays II

题目分析:给定两个数组,求数组的交集。输出结果中的元素不唯一,输出数组可以无序。 解题思路:1)对数组nums1进行排序;2)对数组nums2进行排序;3)遍历数组nums1和nums2中元素,并比较...
  • yzhang6_10
  • yzhang6_10
  • 2016-05-28 18:17:30
  • 2403

LeetCode 350. Intersection of Two Arrays II(数组交集)

原题网址:https://leetcode.com/problems/intersection-of-two-arrays-ii/ Given two arrays, write a functi...
  • jmspan
  • jmspan
  • 2016-05-25 01:35:00
  • 827

[leetcode] 350. Intersection of Two Arrays II 解题报告

题目链接: https://leetcode.com/problems/intersection-of-two-arrays-ii/ Given two arrays, write a funct...
  • qq508618087
  • qq508618087
  • 2016-05-23 13:21:22
  • 2040

Leetcode 349. Intersection of Two Arrays 解题报告 Python Java

1 解题思想这道题就是说,求两个数组的交集,所以做法也很简单: 使用哈希Set存入第一个数组的值 遍历第二个数组,如果第二个的数在Set中出现,那么就是交集(与此同时,因为只能返回一个值,所以出现...
  • MebiuW
  • MebiuW
  • 2016-05-18 23:30:59
  • 5265

leetcode 350 Intersection of Two Arrays II C++

这题用map实现,即相应位置出现即+1,有点计数排序的赶脚。 class Solution { public: vector intersect(vector& nums1, vec...
  • a2331046
  • a2331046
  • 2016-05-23 15:22:30
  • 867

leetcode_349 Intersection of Two Arrays

题目分析:给定两个数组,求数组的交集。输出结果中的元素唯一,输出数组可以无序。 解题思路:1)对数组nums1进行排序;2)对数组nums2进行排序;3)遍历数组nums1和nums2中元素,并比较对...
  • yzhang6_10
  • yzhang6_10
  • 2016-05-28 18:13:55
  • 1647

Leetcode 350. Intersection of Two Arrays II 数组交集2 解题报告

1 解题思想这道题和前两天的那个是一个系列的,只是那一个输出不能重复,而这一题重复了多少个,就输出多少个,所以先看下这个: Leetcode 349. Intersection of Two Arr...
  • MebiuW
  • MebiuW
  • 2016-05-24 11:11:35
  • 3577

Intersection of Two Arrays II两个数组交集(重要!)

https://leetcode.com/problems/intersection-of-two-arrays-ii/ Given two arrays, write a function ...
  • gao1440156051
  • gao1440156051
  • 2016-06-26 23:21:28
  • 1561

349. Intersection of Two Arrays I &&II (Java)

Given two arrays, write a function to compute their intersection.  Example:  Given nums1 = [1, 2, 2,...
  • ruobing2011
  • ruobing2011
  • 2016-05-27 12:56:58
  • 2064

<LeetCode OJ> 349 / 350 Intersection of Two Arrays(I / II)

Total Accepted: 3212 Total Submissions: 6814 Difficulty: Easy Given two arrays, write a functio...
  • EbowTang
  • EbowTang
  • 2016-05-20 00:07:47
  • 1793
收藏助手
不良信息举报
您举报文章:LeetCode(350)Intersection of Two Arrays II
举报原因:
原因补充:

(最多只允许输入30个字)