A、B两个整数集合,设计一个算法求他们的交集

代码留作记录,本人水平有限,看了别人的解法真是自愧不如。

关于此题的详细探讨可以参考:http://blog.csdn.net/thebestdavid/article/details/12056293

/*A、B两个整数集合,设计一个算法求他们的交集,尽可能的高效。*/
#include <iostream>
#include <cstring>
#include <set>
#define M 8
#define N 5
using namespace std;
int main(){
    int A[] = {-1, 2 ,39 ,10, 6, 11, 188, 10};
    int B[] = {39 ,8 , 10, 6, -1};
    set<int> sa;
    set<int> sb;
    for(int i=0;i<M;i++){
        sa.insert(A[i]);
    }
    set<int>::iterator it;
    for(int i=0;i<N;i++){
        if(sa.find(B[i])!=sa.end()){
            sb.insert(B[i]);
        }
    }
    for(set<int>::iterator it=sb.begin();it!=sb.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
    return 0;
}

A、B两个整数集合,设计一个算法求他们的交集

时间: 2024-12-17 19:01:18

A、B两个整数集合,设计一个算法求他们的交集的相关文章

A,B两个整数集合,设计一个算法求他们的交集,尽可能的高效(牛客网)

#include<iostream> using namespace std; /* 1)先使用快速排序,使得两个数组有序: 2)然后利用二分查找的方法,在数组B中查找: 3)其中,注意在数组B中,使用二分查找的起点,是根据上次查找的结果开确定的:这样可以进一步提高速度: */ int Sort(int array[],int low,int high) { int temp=array[low]; int pos=low; while(low<high) { while(array[h

设计一个算法求节点值为x和节点y值得两个节点的最近共同祖先

思想:采用非递归后序遍历二叉树b.当找到节点值为x的节点时将栈中所有节点值存放在anorx数组中(如图所示的二叉树,F节点的anorx为"ACF"),当找到节点值为y的节点时将栈中所有节点值存放在anory数组中(对于如图所示的二叉树,E节点的anory为"ACE"),当两个节点均已找到后,通过比较找到他们最近的公共祖先(对于如图所示的二叉树,F和E节点的最近公共祖先为C),对应的算法如下: int commancestor(BTNode *b,ElementTyp

A、B两个整数集合的交集

rt,这是一个经典问题. 参考1:http://www.sysexpand.com/?path=exercises/array-intersection 参考2:http://leetcode.com/2010/03/here-is-phone-screening-question-from.html 用数组来模拟(本质上说集合中是没有重复元素的,这里用数组来模拟可以用重复元素),A.B数组长度分别为m和n.总的来说,分为以下几种方案. 方案1:两重循环判断(复杂度 O(m*n)) 暴力方法,并

C++程序设计实践指导1.5求两个整数集合并集改写要求实现

改写要求1:改写为单链表结构可以对任意长度整数集合求并集 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode { int data; LinkNode* next; }; class SET { public: struct LinkNode* creat(int x[],int len); struct LinkNode* copy(LinkNode* aHead); int no

剑指offer系列——二维数组中,每行从左到右递增,每列从上到下递增,设计一个算法,找其中的一个数

题目:二维数组中,每行从左到右递增,每列从上到下递增,设计一个算法,找其中的一个数 分析: 二维数组这里把它看作一个矩形结构,如图所示: 1 2 8 9 2 4 9 12 4 7 10 13 6 8 11 15 在做这道题的时候我最先考虑的是每次比较对角线上的元素可能可以取得较好的效果, 以查找9为例, 从1(0,0)开始,1<10,可以得出结论,10在1的右侧或下侧: 1 2 8 9 2 4 9 12 4 7 10 13 6 8 11 15 然后看4(1,1),4<9, 1 2 8 9 2

输入两个实数,用一个函数求出它们之和

/*p180 例7.4 输入两个实数,用一个函数求出它们之和 解题思路:首先要定义add函数,它为float型,它应有两个参数,也应为float型. 特别注意:要对add函数进行声明.*/ #include<stdio.h>int main(){ float add(float x,float y); //对add函数作声明 float a, b, c; printf("please enter a and b:"); //提示输入 scanf("%f,%f&qu

php取两个整数的最大公约数算法大全

php计算两个整数的最大公约数常用算法 <?php//计时,返回秒function microtime_float (){ list( $usec , $sec ) = explode ( " " , microtime ()); return ((float) $usec + (float) $sec );}////////////////////////////////////////////欧几里得算法function ojld($m, $n) { if($m ==0 &a

设计一个算法将一个顺序表逆置

#include<iostream> #include<malloc.h> using namespace std; typedef struct { int length;//保存长度 int data[40];//数组 } SqList; /*算法1:设计一个高效的算法,将顺序表中的所有元素逆置.要求算法空间股咋度为o(1)*/ //初始化顺序表 void initReverse(SqList &s,int *a,int l){ s.length=0; //插入元素 f

请设计一个算法能够完成两个用字符串存储的整数进行相加操作,对非法的输入则返回error

1 #include <iostream> 2 #include <string> 3 #include<algorithm> 4 using namespace std; 5 int main(){ 6 string str1; 7 string str2; 8 while(cin>>str1>>str2){ 9 int size1 = str1.size()-1; 10 int size2 = str2.size()-1; 11 int fl