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 = , j = ;
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 = , right = nums.size();
while (left < right) {
int mid = left + (right - left) / ;
if (nums[mid] == target) return true;
else if (nums[mid] < target) left = mid + ;
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());
}
};

[LintCode] Intersection of Two Arrays 两个数组相交的更多相关文章

  1. [LeetCode] 349. Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  2. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  3. 349 Intersection of Two Arrays 两个数组的交集

    给定两个数组,写一个函数来计算它们的交集.例子: 给定 num1= [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].提示:    每个在结果中的元素必定是唯一的.    我们 ...

  4. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  5. [LintCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...

  6. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  7. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  8. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  9. [leetcode350]Intersection of Two Arrays II求数组交集

    List<Integer> res = new ArrayList<>(); Arrays.sort(nums1); Arrays.sort(nums2); int i1 = ...

随机推荐

  1. Mysql添加外键约束

    简单说一下使用外键的好处 1.完整性约束 比如:用户表中有字段 用户编号(id) , 名称(username)设备表中有字段 设备编号(id) , 设备名称(devicename) 设备属于的用户编号 ...

  2. Socket 通讯

    #import "ViewController.h" #import <sys/socket.h> #import <netinet/in.h> #impo ...

  3. Linux之find命令用于统计信息

    1. 计算当前目录中的文件数: [root@localhost tmp]# find . -type f | wc -l 2. 查找/etc目录中最新的和最旧的文件,以文件时间排序并按年-月-日的格式 ...

  4. OGNL表示式使用和值栈

    另外值得参考博客:http://blog.csdn.net/resigshy/article/details/7560573 OGNL是Object Graphic Navigation Langua ...

  5. ffmpeg处理RTMP流媒体的命令 发送流媒体的命令(UDP,RTP,RTMP)

    将文件当做直播送至live ffmpeg -re -i localFile.mp4 -c copy -f flv rtmp://server/live/streamName   re限制输出速率,按照 ...

  6. Python与Hack之守护进程

    1.什么是守护进程: 在linux或者unix操作系统中,守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件.由于在linux中,每个 ...

  7. java发送短信--httpclient方式

    最近头让我写个发送短信的java程序检测BI系统,检查数据库是否有异常发送,有则发送短信到头的手机里.这里我直说httpclient方式的get请求方式,并且已经有方式的短信的接口了,所以只要再加上参 ...

  8. jquery.color.js的使用

    Jquery本身不支持变色,Jquery Color.js弥补了这缺陷并为animate动画赋予变色效果,如下红变蓝后执行回调再由蓝变红. <!DOCTYPE html> <html ...

  9. PHP历程(封装的增删改查方法)

    db.class.php   主要方法 <?php /** * 数据库配置信息 */ define('DB_HOST','127.0.0.1'); //服务器 define('DB_USER', ...

  10. 持续集成基础-Jenkins(一)

    什么是jenkins: Jenkins是持续集成的一个系统,它是一种软件开发实践活动(经常执行集成,可能每天) 持续集成的价值: 1.减少风险 - 能够尽早的发生问题 2.减少重复过程 - 把重复的东 ...