zoukankan      html  css  js  c++  java
  • [LintCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection.
    Notice

        Each element in the result must be unique.
        The result can be in any order.

    Have you met this question in a real interview?
    Example

    Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
    Challenge

    Can you implement it in three different algorithms?

    LeetCode上的原题,请参见我之前的博客Intersection of Two Arrays

    解法一:

    class Solution {
    public:
        /**
         * @param nums1 an integer array
         * @param nums2 an integer array
         * @return an integer array
         */
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            set<int> s, res;
            for (auto a : nums1) s.insert(a);
            for (auto a : nums2) {
                if (s.count(a)) res.insert(a);
            }
            return vector<int>(res.begin(), res.end());
        }
    };

    解法二:

    class Solution {
    public:
        /**
         * @param nums1 an integer array
         * @param nums2 an integer array
         * @return an integer array
         */
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            vector<int> res;
            int i = 0, j = 0;
            sort(nums1.begin(), nums1.end());
            sort(nums2.begin(), nums2.end());
            while (i < nums1.size() && j < nums2.size()) {
                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;
        }
    };

    解法三:

    class Solution {
    public:
        /**
         * @param nums1 an integer array
         * @param nums2 an integer array
         * @return an integer array
         */
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            set<int> res;
            sort(nums2.begin(), nums2.end());
            for (auto a : nums1) {
                if (binarySearch(nums2, a)) {
                    res.insert(a);
                }
            }
            return vector<int> (res.begin(), res.end());
        }
        bool binarySearch(vector<int> &nums, int target) {
            int left = 0, right = nums.size();
            while (left < right) {
                int mid = left + (right - left) / 2;
                if (nums[mid] == target) return true;
                else if (nums[mid] < target) left = mid + 1;
                else right = mid;
            }
            return false;
        }
    };

    解法四:

    class Solution {
    public:
        /**
         * @param nums1 an integer array
         * @param nums2 an integer array
         * @return an integer array
         */
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            set<int> s1(nums1.begin(), nums1.end()), s2(nums2.begin(), nums2.end()), res;
            set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(res, res.begin()));
            return vector<int>(res.begin(), res.end());
        }
    };
  • 相关阅读:
    PHP计算两个绝对路径的相对路径
    mysql触发器的使用 想让b字段在更新的时候把旧数据保存到a字段中
    LHC大神问的矩阵转置问题
    母牛2年生小牛 5年后并死去的算法
    switch和continue的关系
    逐行读取文件示例
    安装Harbor管理镜像服务
    解决:ElasticSearch ClusterBlockException[blocked by: [FORBIDDEN/12/index read-only / allow delete (api)];
    Jenkins教程(五)构建Java服务Docker镜像
    Nacos高可用集群解决方案-Docker版本
  • 原文地址:https://www.cnblogs.com/grandyang/p/5565633.html
Copyright © 2011-2022 走看看