首页 > 其他 > 详细

LeetCode 349. Intersection of Two Arrays

时间:2016-09-20 23:49:37      阅读:155      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!