350. Intersection of Two Arrays II
Posted Lee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了350. Intersection of Two Arrays II相关的知识,希望对你有一定的参考价值。
Problem:
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.
A possible solution:
class Solution { public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { unordered_map<int,int> m; vector<int> res; for(auto a: nums1) m[a]++; for(auto a: nums2) { if(m[a]-- >0) res.push_back(a); } return res; } };
以上是关于350. Intersection of Two Arrays II的主要内容,如果未能解决你的问题,请参考以下文章
350. Intersection of Two Arrays II
Leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II
350. Intersection of Two Arrays II