[LeetCode] 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把nums1都放进去,然后遍历nums2的元素,如果在set中存在,说明是交集的部分,加入结果的set中,最后再把结果转为vector的形式即可:
解法一:
- class Solution {
- public:
- vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
- set<int> s(nums1.begin(), nums1.end()), res;
- for (auto a : nums2) {
- if (s.count(a)) res.insert(a);
- }
- return vector<int>(res.begin(), res.end());
- }
- };
我们还可以使用两个指针来做,先给两个数组排序,然后用两个指针分别指向两个数组的开头,然后比较两个数组的大小,把小的数字的指针向后移,如果两个指针指的数字相等,那么看结果res是否为空,如果为空或者是最后一个数字和当前数字不等的话,将该数字加入结果res中,参见代码如下:
解法二:
- class Solution {
- public:
- 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;
- }
- };
我们还可以使用二分查找法来做,思路是将一个数组排序,然后遍历另一个数组,把遍历到的每个数字在排序号的数组中用二分查找法搜索,如果能找到则放入结果set中,这里我们用到了set的去重复的特性,最后我们将set转为vector即可:
解法三:
- class Solution {
- public:
- 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;
- }
- };
或者我们也可以使用STL的set_intersection函数来找出共同元素,很方便:
解法四:
- class Solution {
- public:
- 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());
- }
- };
类似题目:
参考资料:
https://leetcode.com/discuss/103345/three-java-solutions
https://leetcode.com/discuss/103224/my-c-solution-with-sort
https://leetcode.com/discuss/103295/my-c-solutions-using-set-and-unordered_set
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Intersection of Two Arrays 两个数组相交的更多相关文章
- [LeetCode] 349. Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...
- [LintCode] Intersection of Two Arrays 两个数组相交
Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...
- 349 Intersection of Two Arrays 两个数组的交集
给定两个数组,写一个函数来计算它们的交集.例子: 给定 num1= [1, 2, 2, 1], nums2 = [2, 2], 返回 [2].提示: 每个在结果中的元素必定是唯一的. 我们 ...
- [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 ...
- [LeetCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- [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 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- [LintCode] Intersection of Two Arrays II 两个数组相交之二
Given two arrays, write a function to compute their intersection.Notice Each element in the result s ...
- LeetCode Intersection of Two Arrays
原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...
随机推荐
- MVP社区巡讲-云端基础架构:12月5日北京站 12月12日上海站
紧跟当今的技术发展趋势还远远不够,我们要引领变革!加入本地技术专家社区,获取真实案例.实况培训演示以及探讨新一代解决方案.在此活动中,您将: 了解如何运用开源(OSS)技术.Microsoft 技术及 ...
- How to implement equals() and hashCode() methods in Java[reproduced]
Part I:equals() (javadoc) must define an equivalence relation (it must be reflexive, symmetric, and ...
- php内核分析(四)-do_cli
这里阅读的php版本为PHP-7.1.0 RC3,阅读代码的平台为linux # main 把剩下的代码增加了下注释全部贴出来了(这个是简化后的main函数,去掉了一些无关紧要的代码段): int m ...
- HTML5 网络拓扑图整合 OpenLayers 实现 GIS 地图应用
在前面<百度地图.ECharts整合HT for Web网络拓扑图应用>我们有介绍百度地图和 HT for Web 的整合,我们今天来谈谈 OpenLayers 和 HT for Web ...
- 『.NET Core CLI工具文档』(七)dotnet-new
说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-new 翻译:dotnet-new 名称 dotnet-new -- 创建一个新的 .NET Core 项 ...
- SQL SERVER 竖表变成横表
现有数据如下: Sql: select a.MODELID, max( case a.PNAME when'计划开始' then a.PVALUE end) as RStart, max( case ...
- Delphi_03_Delphi_Object_Pascal_基本语法_01
这次是一个基本语法的第一部分,包括变量.变量初始化.常量.运算符.字符串等内容. { 本程序演示 Delphi Pascal 的基本语法 1.变量及变量的初始化 2.常量 3.运算符 3. 4. } ...
- Spring MVC中的ModelMap作用及用法
ModelMap的作用: ModelMap对象主要用于传递控制方法传递数据到结果页面.类似于request的setAttribute方法的作用. 所以我们要想在jsp页面获取数据,只要将数据放到Mod ...
- [moka同学笔记]PHPexcel之excel导出和导入
原案例来自http://www.sucaihuo.com/有修改 1.目录结构(文件不用解释,应该都可以看得懂,直接看代码)
- 脚本化CSS类-HTML5 classList属性
HTML元素可以有多个CSS类名,class属性保存了一个用空格隔开的类名列表.标识符class在JavaScript中是保留字,所以在JavaScript中可以用className. //如下代码设 ...