// Type your C++ code and click the "Run Code" button! // Your code output will be shown on the left. // Click on the "Show input" button to enter input data to be read (from stdin). #include <iostream> #include <vector> using namespace std; // find the intersection of two array vector<int> intersect(vector<int> a, vector<int> b) { vector<int> intersection; vector<int>::iterator ai = a.begin(); vector<int>::iterator bi = b.begin(); while(ai != a.end() && bi != b.end()) { if(*ai > *bi) { bi ++; } else if(*ai < *bi) { ai ++; } else { intersection.push_back(*ai); ai ++; bi ++; } } return intersection; } void print_vector(vector<int> vet) { for(vector<int>::iterator iter = vet.begin(); iter != vet.end(); iter++) { cout<<*iter<<" "; } cout<<endl; } int main() { // test data vector<int> alist; vector<int> blist; for(int i = 0; i < 100; i+=3) alist.push_back(i); for(int i = 0; i < 100; i+=2) blist.push_back(i); cout<<"alist: "<<endl; print_vector(alist); cout<<"blist: "<<endl; print_vector(blist); cout<<"intersection: "<<endl; print_vector(intersect(alist, blist)); return 0; }
欢迎关注微信公众号——计算机视觉:
相关推荐
自己编写的程序,实现两个有序数组求交集。例如a={1,2,3,4,5,6,7},b={1,3,5,8,9,11},则结果为{1,3,5}
查找数组中的重复元素,且时间复杂度为O(n)
两个数组的交集给定两个数组,编写一个函数来计算它们的交集。示例 1:输出:[2]示例 2:输出:[9,4]def intersection(self, nums
本文实例讲述了JavaScript获取两个数组交集的方法。分享给大家供大家参考。具体如下: 这里传入的数组必须是已经排过序的 /* finds the intersection of * two arrays in a simple fashion. * * PARAMS * a - ...
本文通过多种实现方式给大家介绍了JS计算两个数组的交集、差集、并集、补集 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
从数组1的尚未比较的元素中拿出第一个元素array1(i),用array1(i)与array2(j)进行比较(其中j>i且j的长度),可能出现下面两种情况, 1. 数组2中找到了一个与array1(i)相等的元素,则将array2(j)与array2(i)进行...
示例 1:输出:[2,2]示例 2:输出:[4,9]vector<int> intersect(vector<int>& nums1, vector<int>&
本文实例讲述了Python实现求两个数组交集的方法。分享给大家供大家参考,具体如下: 一、题目 给定两个数组,编写一个函数来计算它们的交集。 例1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 例2: ...
java 二个数组的交集,算法 java 二个数组的交集,算法
Java 实例 - 计算两个数组交集源代码-详细教程.zip
数组求交集(C语言),入门小程序,适合C语言入门的小练习 数组求交集(C语言),入门小程序,适合C语言入门的小练习 数组求交集(C语言),入门小程序,适合C语言入门的小练习 数组求交集(C语言),入门小程序...
利用指针来实现动态数组,求两个集合的交集和并集。(要求用动态数组来实现)依次分别输入数组A、B长度,并输入A,B中元素,即可得到交集并集
主要介绍了Java 数组交集的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
// res得到两个数组中交集 [2,2,3,4] var res = arr1.filter(function (item, index) { // 那arr1中的数据 去arr2数组中检测在arr2是否存在 // arr2.indexOf(item) 检测arr2中否是有 item这个数据 // 有数据就...
在 MATLAB 中,有一个命令叫做“intersect”,它可以找到两个向量的集合交集(公共元素)。 但是,如果我想比较两个以上的向量并找出它们的交集,则此命令不起作用。 因此,我决定编写这个名为“intersect2”的函数...
用C#简单实现了对字符串数组求交集并集,定义类
js代码-(算法)两个数组交集
两个数组的交集(set去重后找交集)1