LeetCode 349. Intersection of Two Arrays

时间:2016-09-20 23:49:37   收藏:0   阅读:156

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

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

Note:

 

 

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;
    }
};

 

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!