找两个数组的交集(不要多想,考虑啥序列之类的,就是简单的两堆数求交集啊!!!最后去个重就好了)

//LeetCode里用到很多现成函数的时候,苦手だな~
//这个题的思路是,先sort,把两个vector进行排序
//然后同时遍历两个字符串,找到相等的数字就放在结果vector里
//最后unique,求vector里独一无二的数字
//erase,得到没有重复的结果 class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
// Write your code here
sort(nums1.begin(), nums1.end());
sort(nums2.begin(), nums2.end()); vector<int> intersect;
vector<int>::iterator it1 = nums1.begin(), it2 = nums2.begin();
while ((it1 != nums1.end()) && (it2 != nums2.end()))
{
if (*it1 < *it2) it1++;
else if (*it1 > *it2) it2++;
else
{
intersect.push_back(*it1);
it1++; it2++;
}
} auto last = unique(intersect.begin(), intersect.end());
intersect.erase(last, intersect.end());
return intersect;
}
};

【easy】349. Intersection of Two Arrays的更多相关文章

  1. 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...

  2. 【leetcode】349. Intersection of Two Arrays

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

  3. 【leetcode】350. Intersection of Two Arrays II

    problem 350. Intersection of Two Arrays II 不是特别明白这道题的意思,例子不够说明问题: 是按顺序把相同的元素保存下来,还是排序,但是第二个例子没有重复... ...

  4. 【一天一道LeetCode】#349. Intersection of Two Arrays

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  5. 【LeetCode】350. Intersection of Two Arrays II 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 Java排序+双指针 Python排序+双指针 Python解 ...

  6. 【一天一道LeetCode】#350. Intersection of Two Arrays II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  7. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  8. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  9. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

随机推荐

  1. 洛谷 AT667 【天下一人力比較】

    题目链接 https://www.luogu.org/problemnew/show/AT667 题目描述啥的,都看不懂 就一句翻译: 读入若干个字符串,找到字典序第7的字符串并输出. 感谢@da32 ...

  2. Python + Tornado 搭建自动回复微信公众号

    1 通过 pip 安装 wechat-python-sdk , Requests 以及 Tornado pip install tornado pip install wechat-sdk pip i ...

  3. linux下开启、关闭、重启mysql服务命令

    一. 启动1.使用 service 启动:service mysql start2.使用 mysqld 脚本启动:/etc/inint.d/mysql start3.使用 safe_mysqld 启动 ...

  4. red()、redinle()、redlines()三者之间的关系

    # 关于read()方法: # 1.读取整个文件,将文件内容放到一个字符串变量中 # 2.如果文件大于可用内存,不可能使用这种处理 file_object = open("a.txt&quo ...

  5. nfs 权限问题

    nfs-server 上面的共享文件的权限修改后 到nfs-client 上面挂载后的权限不能刷新的问题 修改/etc/idmap.conf Domain = local.domain.edu #打开 ...

  6. Codeforces1100F Ivan and Burgers 【整体二分】【线性基】

    题目分析: 一道近似的题目曾经出现在SCOI中,那题可以利用RMQ或者线段树做,这题如果用那种做法时间复杂度会是$log$三次方的. 采用一种类似于整体二分的方法可以解决这道题. 将序列的线段树模型建 ...

  7. shell脚本自动化部署服务

    shell脚本自动化部署 !/bin/bash #export PATH=$PATH:/export/maven/bin run_flag_dir="/data0/shell/deploy_ ...

  8. consul如何限制注册的ip

    假设当前服务器的ip是:192.168.56.130 允许 所有ip 注册 consul agent -server -ui -bootstrap-expect=1 -data-dir=/usr/lo ...

  9. Magento 2 Error: A technical problem with the server created an error. Try again to continue what you were doing. If the problem persists, try again later.

    有时在Magento上进行调试会很烦人,特别是如果出现了一些问题,而不是在你实际编写代码时. 这是Magento管理面板上发生的事情.截至目前,它可能是三个原因之一,让我们讨论一下: 1.管理员密码 ...

  10. 小程序运行报错:errMsg: "request:fail url not in domain list"

    错误原因: 报错提示说请求的url不在域名列表里,应该是还没有配置服务器域名 解决方法: 可点击开发者工具右上角 详情-项目设置-不校验合法域名.web-view(业务域名).TLS 版本以及 HTT ...