[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());
    }
};

本文转自博客园Grandyang的博客,原文链接:两个数组相交[LintCode] Intersection of Two Arrays ,如需转载请自行联系原博主。

相关文章
|
2月前
|
关系型数据库 分布式数据库 数据库
又一国赛来啦!第2届PolarDB数据库创新设计赛开放报名!|全国普通高校学科竞赛排行榜入选赛事等你来战
「2025全国大学生计算机系统能力大赛——第2届PolarDB数据库创新设计赛」已启动报名,面向全国高校本、专、硕博学生开放。赛事聚焦PolarDB-PG向量计算优化,设初赛与决赛,总奖金达25万元。旨在提升数据库系统设计与创新能力,推动产学研融合,欢迎踊跃参赛!
|
11月前
|
机器学习/深度学习 人工智能 编解码
Inf-DiT:清华联合智谱AI推出超高分辨率图像生成模型,生成的空间复杂度从 O(N^2) 降低到 O(N)
Inf-DiT 是清华大学与智谱AI联合推出的基于扩散模型的图像上采样方法,能够生成超高分辨率图像,突破传统扩散模型的内存限制,适用于多种实际应用场景。
304 21
Inf-DiT:清华联合智谱AI推出超高分辨率图像生成模型,生成的空间复杂度从 O(N^2) 降低到 O(N)
|
Web App开发 JSON 前端开发
使用 React 和 NodeJS 创建一个全栈项目
在本文中,我将使用 React 和 NodeJS 创建一个全栈项目。介绍下如何让 Node.js 作为 web 服务器来加载静态资源,如何让 React 程序可以直接调用 Node API。
993 0
|
前端开发 Java 微服务
SpringCloud微服务之间传输文件
SpringCloud微服务之间传输文件
431 1
|
网络协议 安全 算法
谷歌出品!读懂 QUIC 协议:更快、更高效的通信协议
谷歌出品!读懂 QUIC 协议:更快、更高效的通信协议
3262 0
|
数据挖掘 索引 Python
【100天精通Python】Day57:Python 数据分析_Pandas数据描述性统计,分组聚合,数据透视表和相关性分析
【100天精通Python】Day57:Python 数据分析_Pandas数据描述性统计,分组聚合,数据透视表和相关性分析
401 0
|
缓存 Linux
Linux 清空缓存命令
某些时候需要把linux 的缓存清理一下。使用时需要区分参数的不同
588 0
|
存储 机器学习/深度学习 人工智能
数据结构(严蔚敏版)——第一章 绪论
本文章是观看青岛大学——王桌老师讲(严蔚敏版)数据结构视频所做的总结, 包含:1、基本概念 2、抽像数据类型的定义与实现 3、算法时间复杂度的分析 4、算法的空间复杂度 以及例题复数的定义与实现(包含两种代码:C语言、JAVA)
434 1
|
算法 C++
|
存储 算法 计算机视觉
使用Roberts算子进行图像分割(Matlab自编程实现)
使用Roberts算子进行图像分割(Matlab自编程实现)
553 0
使用Roberts算子进行图像分割(Matlab自编程实现)