zoukankan      html  css  js  c++  java
  • leetcode 349. Intersection of Two Arrays

    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.

    判断两个数组的交集,去掉重复的。我们可以用set   也可以用下面的方法

    class Solution {
    public:
        static bool myfunction (int i, int j) {
            return (i==j);
        }
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            vector<int> v;
            map<int,int> mp;
            for (int i = 0; i < nums1.size(); ++i) mp[nums1[i]]++;
            for (int i = 0; i < nums2.size(); ++i) {
                if (mp[nums2[i]] > 0) v.push_back(nums2[i]);
            }
            sort(v.begin(), v.end());
            std::vector<int>::iterator it;
            it = unique(v.begin(), v.end());
            v.resize( std::distance(v.begin(),it) );
            return v;
        }
    };
  • 相关阅读:
    bzoj3996
    bzoj3157 3516
    bzoj1937
    bzoj1532
    bzoj3572
    bzoj1453
    bzoj3205
    bzoj2595
    关于高斯消元解决xor问题的总结
    linux查找和替换命令
  • 原文地址:https://www.cnblogs.com/pk28/p/7387684.html
Copyright © 2011-2022 走看看