【第21期】观点:人工智能到底用 GPU?还是用 FPGA?

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, 2, ...

Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins.  </p

LeetCode-349&350.Intersection of Two Arrays

https://leetcode.com/problems/intersection-of-two-arrays/ Given two arrays, write a function to co...

[LeetCode]160.Intersection of Two Linked Lists

【题目】 Write a program to find the node at which the intersection of two singly

[LeetCode]349. Intersection of Two Arrays(两数组的交集)

349. Intersection of Two Arrays点击查看350. Intersection of Two Arrays II原题链接 Given two arrays, write a...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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