349. Intersection of Two Arrays

原创 2016年06月02日 10:52:44

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.
使用unordered_set    Runtime: 132 ms

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> hs1;
        unordered_set<int> hs2;
        vector<int> vi;
        int len1=nums1.size();
        int len2=nums2.size();
        for(int i=0;i<len1;i++)
            hs1.insert(nums1[i]);
        for(int i=0;i<len2;i++)
            hs2.insert(nums2[i]); 
            
        for(auto it1=hs1.begin();it1!=hs1.end();it1++)
         for(auto it2=hs2.begin();it2!=hs2.end();it2++)
             if(*it1==*it2)
                vi.push_back(*it1);
        
        return vi;
        
    }
};


Runtime: 32 ms


class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> set1;
        int len2=nums2.size();
        for(int i=0;i<len2;i++)
            if(find(nums1.begin(),nums1.end(),nums2[i])!=nums1.end()) 
                set1.insert(nums2[i]);
        return vector<int> (set1.begin(),set1.end());
        
    }
};

Runtime: 8 ms

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        vector<int> vi;
        unordered_set<int> m(nums1.begin(),nums1.end());
        for(auto a:nums2)
            if(m.count(a))
            {
                vi.push_back(a);
                m.erase(a);
            }
        return vi;
    }
};


排序算法 Runtime: 16 ms

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        std::sort(nums1.begin(),nums1.end());
        std::sort(nums2.begin(),nums2.end());
        vector<int> res;
        int len1=nums1.size();
        int len2=nums2.size();
        int i=0,j=0;
        while(i<len1&&j<len2)
        {
            if(nums1[i]<nums2[j])
                i++;
            else if(nums1[i]>nums2[j])
                j++;
            else
            {
                if(res.empty()||res.back()!=nums1[i])
                    res.push_back(nums1[i]);
                i++;
                j++;
            }
        }
        return res;
    }
};







版权声明:更多请参考:http://blog.csdn.net/yangquanhui1991,我们一起每天进步一点点!

LeetCode 349. Intersection of Two Arrays

题目: Given two arrays, write a function to compute their intersection.Example: Given nums1 = [1, 2,...

leetcode解题之349 & 350. Intersection of Two Arrays Java版(求数组交集))

350. Intersection of Two Arrays II leetcode解题之349. Intersection of Two Arrays & 350. Intersection ...

leetcode349~Intersection of Two Arrays

Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2...

【Leetcode】349. Intersection of Two Arrays

方法一: 思路: 为避免重复,用Set存储后再存入结果数组。 遍历nums1,对nums2的每一个元素,若找到与nums1对应元素相等的元素且在Set中未出现,则加入Set。 public clas...

349. Intersection of Two Arrays --双指针、字典、二分查找、集合

349. Intersection of Two Arrays很经典的题目,可以使用双指针、字典、二分查找、集合来实现Given two arrays, write a function to com...

leetcode_349 Intersection of Two Arrays

题目分析:给定两个数组,求数组的交集。输出结果中的元素唯一,输出数组可以无序。 解题思路:1)对数组nums1进行排序;2)对数组nums2进行排序;3)遍历数组nums1和nums2中元素,并比较对...

349. Intersection of Two Arrays I &&II (Java)

Given two arrays, write a function to compute their intersection.  Example:  Given nums1 = [1, 2, 2,...

[LeetCode-349] Intersection of Two Arrays(java)

Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2,...

349. Intersection of Two Arrays [easy] (Python)

题目链接https://leetcode.com/problems/intersection-of-two-arrays/题目原文 Given two arrays, write a functi...

LeetCode349 Intersection of Two Arrays

原题Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:349. Intersection of Two Arrays
举报原因:
原因补充:

(最多只允许输入30个字)