问题描述:
有两个有序整数集合a和b,写一个函数找出它们的交集?
几种解决方案:
一:
import java.util.Arrays;
public class Test {
public static void main(String args[]){
int[] b = {4, 6, 7, 7, 7, 7, 8, 8, 9, 10, 100, 130, 130, 140, 150};
int[] a = {2, 3, 4, 4, 4, 4, 7, 8, 8, 8, 8, 9, 100, 130, 150, 160};
int[] c = intersect(a, b);
System.out.println(Arrays.toString(c));
}
public static int[] intersect(int[] a, int[] b) {
if(a[0] > b[b.length - 1] || b[0] > a[a.length - 1]) {
return new int[0];
}
int[] intersection = new int[Math.max(a.length, b.length)];
int offset = 0;
for(int i = 0, s = i; i < a.length && s < b.length; i++) {
while(a[i] > b[s]) {
s++;
}
if(a[i] == b[s]) {
intersection[offset++] = b[s++];
}
while(i < (a.length - 1) && a[i] == a[i + 1]) {
i++;
}
}
if(intersection.length == offset) {
return intersection;
}
int[] duplicate = new int[offset];
System.arraycopy(intersection, 0, duplicate, 0, offset);
return duplicate;
}
}
import java.util.ArrayList;
public class jiaoji {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] b = { 4, 6, 7, 7, 7, 8, 8, 9, 10, 100, 130, 140, 150 };
int[] a = { 2, 3, 4, 4, 4, 7, 8, 8, 8, 8, 9, 100, 130, 150, 160 };
ArrayList c = sect(a, b);
for (int i = 0; i < c.size(); i++) {
System.out.print(c.get(i) + ",");
}
}
public static ArrayList sect(int[] a, int[] b) {
int k = 0;
int startposb = 0;
if (a[0] > b[b.length - 1] || b[0] > a[a.length - 1]) {
return new ArrayList();
}
ArrayList arraylist = new ArrayList();
for (int i = 0; i < a.length; i++) {
for (int j = startposb; j < b.length; j++) {
if (a[i] == b[j]) {
arraylist.add(k, a[i]);
k++;
startposb = j + 1;
break;
}
}
}
return arraylist;
}
}
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class NumberCross {
public static void main(String[] args) {
int a[] = {1,3,4,5,6,7,499,199};
int b[] = {3,299,199,8,9,499};
Set<Integer> list = new HashSet<Integer>();
for(int i=0; i<a.length; i++) {
list.add(a[i]);
}
for(int j=0; j<b.length; j++) {
list.add(b[j]);
}
for(Iterator<Integer> iter=list.iterator(); iter.hasNext();) {
System.out.print(iter.next() + " ");
}
}
}
分享到:
相关推荐
求两集合的交集 求两个集合的交集 CPP文件
在 int_set 类中,我们还定义了三个函数,用于实现集合的交集、并集和差集运算。jiao 函数,用于计算两个集合的交集;bing 函数,用于计算两个集合的并集;cha 函数,用于计算两个集合的差集。 最后,我们在 main ...
1.有序顺序表的元素按照从小到大有序存储; 2.实现有序顺序表的类模板,它的操作如下: ...3.用有序顺序表表示集合,实现两个有序顺序表的并和交(并和交仍是有序顺序表)并分析它们的时间复杂度;
设 A={a1,a2,…,an}, B={b1,b2,…,bn}是整数集合,其中 m=O(log n)。要求设计一个算法求集 合 C=A∩B。提示:使用二分查找技术。
集合的交集、并集、差集运算是考察的常规内容,这里给出交集的代码,并集跟差集的类似
交集是由属于集合A且属于集合B的所有元素组成的集合,记作A∩B。补集是由全集U中不属于集合A的所有元素组成的集合,记作U\A。 六、集合的性质 集合的性质包括交集的性质和并集的性质。交集的性质包括交换律、结合律...
已知所给集合 A 和 B,求 A 与 B 的并集 C(C=A∪B)。 使学生更深刻理解集合中并运算定义,并掌握通过计算机语言实现集合并运算的基本方法。 主要仪器设备及耗材 PC,Dev-C++
采用堆排序法求两个集合的并集,交集和差级
⑹ 求集合A与B的对称差E=(A-B)∪(B-A) 的函数,并输出集合D的元素; ⑺ 设计一个菜单,具有输入集合元素、求集合A、B的交C、求集合A、B的并D、求集合A与B的对称差E、退出等基本的功能。 测试数据:由读者自定,但...
用线性表实现集合的求交集和并集的运算 (*^__^*)
上面以两个集合为例说明了交集和并集的求法,事实上,上述算法可以很方便的扩展到3个或3个以上的集合 的求交集和求并集。另外求并集时,由于哈希表的值(VALUE)部分不需要用到,所以这个数据结构也可以更换为 ...
已知两个集合,求这两个集合的交集和并集的MATLAB代码,txt文档
N个集合求交集C语言代码,任意输入n个集合求交集
已知两个单链表 LA 和 LB 分别表示两个集合,其元素递增排序,设计算法求出 LA 和 LB 的交集 C ,要求 C 同样以元素递增的单链表形式存储。
求两个整型(int型)集合的交集,所用到的语言为C#语言。
求交集和非交集 求交集和非交集 求交集和非交集
实现整数集合类 要求: 1、类中含两个私有变量,集合中元素的个数和集合中元素组成的数组。 2、用Set函数输入,Show函数输出结果(按从小到大的顺序输出各个元素)。 3、实现运算符+的重载,表示两个集合的并集。实现...
一个用c语言求两个整数集合的交集的程序,其中使用的函数的调用以及文件的使用,其中调用的文本请自行创建,很简单的
义一个整数集合类integerSet。这种类型的对象可以存储10个20~80之间的整数,即他的内部有一个整型数组存储数据。编程: (1) 判断两个inergerSet类对象S1和S2是否相等。提示:集合相等的前提是所有元素相等。 (2)...
cout请输入A集合中的数,以数字0结束:"; while(1){ cin>>x; if(x==0) break; a.insert(i,x); i++; cout; } cout; cout请输入B集合中的数,以数字0结束:"; while(1){ cin>>y; if(y==0) break; b....