码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 349. Intersection of Two Arrays

时间:2016-09-20 23:52:22      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

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.

 

 

Subscribe to see which companies asked this question

题目要求:给两个数组,求他们的合集
 
class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        map<int, int> map;
        vector<int> ret;
        for (auto &e : nums1)
            map[e] = 1;
        for (auto &e : nums2)
        {
            if (map.find(e)!=map.end()&&map[e]==1)
            {
                ret.push_back(e);
                ++map[e];
            }
        }
        return ret;
    }
};

 

LeetCode 349. Intersection of Two Arrays

标签:

原文地址:http://www.cnblogs.com/csudanli/p/5890922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!