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;
        }
    };
  • 相关阅读:
    poj 1634
    poj 2153
    POJ 1693
    poj 1789
    POJ 2676
    vue 路由
    用 node.js 创建第一个Hello World
    js原生Ajax 的封装和原理
    BFC原理
    怎么理解js的面向对象编程
  • 原文地址:https://www.cnblogs.com/pk28/p/7387684.html
Copyright © 2011-2022 走看看