有两个数组a{1,5,8,10,14,15,17,18,20,22,24,25,28}和b{2,4,6,8,10,12},如何求出他们之间的交集?要求效率越高越好
注:数组都是从小到大排序好的
注:数组都是从小到大排序好的
解决方案 »
- 急求用户注册判断是否有重名用户的代码,我的不能做判断,只会添加,根本不会判断!!!
- socket报SocketTimeoutException
- 优先级问题!!
- 内部类中为什么不能声明类变量与类方法呢???
- mysql中如何动态创建表?
- 请高手帮忙
- 我在使用DataTable时,在表格中加入<h:commandLink action="bean.editAction">
- 编译不报错 程序运行也没问题 但多了两个NOTE
- 怎样利用java多线程,在一个Applet上实现两个图片交换显示?
- jre与jdk区别何在?
- 不能抛出RuntimeException类型的异常
- 求简单正则表达式字符串替换~~~
那么可以直接对比a和b的元素
i,j是a,b中元素的当前位置
while(i<a.length && j < b.length)
{
if(a[i] < b[j])
{
i++;
}
else if (a[i] == b[j])
{
输出这个元素;
i++;
j++;
}
else
{
j++;
}
}
int[] b = {2,4,6,8,10,12};List list = new ArrayList(Arrays.asList(a));
list.retainAll(Arrays.asList(b)); // list 中的就是交集了.
{
public static void main(String[] args)
{
Set<Integer> s1 = new HashSet<Integer>();
Set<Integer> s2 = new HashSet<Integer>();
int[] a = {1,5,8,10,14,15,17,18,20,22,24,25,28};
int[] b = {2,4,6,8,10,12}; for(int i =0;i<a.length;i++) {
s1.add(a[i]);
} for(int i =0;i<b.length;i++) {
s2.add(b[i]);
} s1.retainAll(s2); //求交集
System.out.println(s1);
}
}
结果[8,10]
import java.util.*;public class STest {
private static int count, index;
public static void getDuplicated(int[] a, int[] b){
Set<Integer> set = new LinkedHashSet<Integer>();
for(int i=0; i<a.length; i++)
set.add(a[i]);
for(int j=0; j<b.length; j++){
int setSize = set.size();
if(j>0){
if(b[j] == b[j-1])
continue;
}
set.add(b[j]);
if(set.size() == setSize){
b[index] = b[j];
count ++;
index ++;
}
}
}
public static void main(String[] args) {
int[] first = {1,5,8,8,10,100,14,15,70,70,17,18,18,20,22,24,25,28};
int[] second = {2,4,4,4,4,6,8,8,8,8,10,12};
getDuplicated(first, second);
for(int i=0; i<count; i++)
System.out.print(second[i] +" ");
}
}
如果用set的话,add重复的进去返回值是false,就不需要再判断size了
public static void main(String args[]){
int a[] = {1,5,8,10,14,15,17,18,20,22,24,25,28};
int b[] = {2,4,6,8,10,12};
for(int i = 0; i < b.length; i++){
for(int j = 0; j < a.length; j++){
if(b[i] == a[j]){
System.out.print(" " + b[i]);
break;
}
else
continue;
}
}
}
}
public class ArrayIntersection {
public static void main(String[] args) {
int[] a = { 1, 5, 8, 10, 14, 15, 17, 18, 20, 22, 24, 25, 28 };
int[] b = { 2, 4, 6, 8, 10, 12 };
int lenA = a.length;
int lenB = b.length;
for (int ai = 0, bi = 0; ai < lenA && bi < lenB;) { if (a[ai] < b[bi])
ai++;
else if (a[ai] > b[bi])
bi++;
else {
System.out.println(a[ai]);
ai++;
bi++;
}
}
}
}
时间复杂度应该是O(min(a,b))吧.
当一次a++,下次b++的时候就不是了
int[]results=int[a.length];
int c=0;
for(int i=0;i<a.length;++i)
{
for(int j=0;j<b.length;++j)
{
if(a[i]<b[j])break;
if(a[i]==b[j])
{results[c]=a[i];++c}
}
}
{
int []results;
int count=0;
int i=j=0;
while(i<a.length&&j<b.length)
{
if(a[i]==a[j])
{
results[c]=a[i];
count++;
} }
for(int k=0;k<results.length;k++)
System.out.print(results[k]+" ");
}
去找这个类,直接求的交集!
public static void go(int[] a, int[] b) {
int i = 0;
int j = 0; while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
i++;
} else if (a[i] > b[j]) {
j++;
} else {
System.out.print(a[i] + " ");
i++;
j++;
}
}
} public static void main(String[] args) {
int[] a = { 1, 5, 8, 10, 14, 15, 17, 18, 20, 22, 24, 25, 28 };
int[] b = { 2, 4, 6, 8, 10, 12 };
go(a, b);
}
}
很明显的 for 循环中的i,j一次只能是其中一个Counter自加(if语句)
所以 ,最好的情况是O(min(a,b)),最坏的情况是O(a+b)
好:a[]={1,3,5},b[]={9,10,12}
坏:a[]={1,12},b[]={3,5,9,10,}
哈希表的复杂度应该是m+n=max(m,n)复杂度,线性的.
int k = 0;
public int[] jiao(int[] x, int[] y){ //求两个集合的交集
int[] z = new int[max(x.length, y.lengh)];
for(int i=0; i<x.length; i++){
for(int j=0; j<y.length; j++){
if(y[j] > x[i]) break;
if(y[j] == x[i]) {
z[k++] = y[j];
}
}
}
return z;
}
}
//#67楼code改进int k = 0;
public int[] jiao(int[] x, int[] y){ //求两个集合的交集
int[] z = new int[max(x.length, y.lengh)];
int i,j = 0;
for(i=0; i<x.length, j<y.length; i++){
for(j=0; j<y.length; j++){
if(y[j] > x[i]) break;
if(y[j] == x[i]) {
z[k++] = y[j];
}
}
}
return z;
}
}
up按理说效率蛮高了
只是length可以只计算一次
那么可以直接对比a和b的元素
i,j是a,b中元素的当前位置
Java codewhile(i <a.length&& j < b.length)
{if(a[i] < b[j])
{
i++;
}elseif (a[i]== b[j])
{
输出这个元素;
i++;
j++;
}else
{
j++;
}
}
例如:
Integer one=new Integer(1),
two=new Integer(2),
three=new Integer(3),
four=new Integer(4),
five=new Integer(5),
six=new Integer(6);
HashSet A=new HashSet(),
B=new HashSet(),
tempSet=new HashSet();
A.add(one);
A.add(two);
A.add(three);
A.add(four);
B.add(one);
B.add(two);
B.add(five);
B.add(six);
tempSet=(HashSet)A.clone();
A.removeAll(B); //A变成调用该方法之前的A集合与B集合的差集
B.removeAll(tempSet); //B变成调用该方法之前的B集合与tempSet集合的差集
B.addAll(A); //B就是最初的A与B的交集
public Object doInHibernate ( Session session ) throws HibernateException, SQLException {
Query query = session.createQuery ( hql ) ;
query.setFirstResult ( offset ) ;
query.setMaxResults ( length ) ;
List list = query.list ( ) ;
return list ;
}
}) ;
return list ;
}