349. Intersection of Two Arrays 两个数组交集

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.

class Solution {
public:
	vector intersection(vector& nums1, vector& nums2) {
		vector res;
		unordered_map s;
		if (nums1.size() == 0 || nums2.size() == 0){
			return res;
		}
		for (int n : nums1){
			s[n] = true;
		}

		for (int n : nums2){
			if (s.count(n)&&s[n]==true){
				res.push_back(n);
				s[n] = false;
			}
		}
		return res;
	}
};


你可能感兴趣的:(leetcode)