例:
1,3,5,6,7,9
2,4,6,7,8,9
如何能够最快的取出交集.
有没有什么好的算法.我不想去一个一个的循环比较.
1,3,5,6,7,9
2,4,6,7,8,9
如何能够最快的取出交集.
有没有什么好的算法.我不想去一个一个的循环比较.
解决方案 »
- 关于IOException
- 显示图像文件问题
- 关于类的设计
- SMTP协议,返回正确,但为什么收不到邮件呢?
- 大家帮忙看看一个奇怪的问题,关于动作监听的
- 请问我的java程序在绘制一个GIF时汉字全是方框,请问怎么解决?linux下,我起了X了
- 求JBUILDER中文帮助文档!
- 关于菜单的奇怪问题
- 关于包的问题(什么情况下才能访问同一目录下另一文件的类?)
- 为什么说我没有匹配的驱动程序哪?(内附源代码)
- lookupPrintServices(null, null) 找不到打印服务!!急!!!
- 讨论,为什么很多人都不定义public变量? 而要另外写get,set函数来设置private变量的值
int[] a = {1,3,5,6,7,9};
int[] b = {2,4,6,7,8,9};
Set s = new HashSet();
for (int i=0, j=0; i<a.length && j<b.length; ) {
if (a[i]==b[j]) {
s.add(""+a[i]);
i++;
j++;
} else if (a[i] > b[j]){
j++;
} else {
i++;
}
}
if (s.size()==0) {
Sytem.out.println("no intersection");
} else {
System.out.println(Arrays.toString(s.toArrays()));
}
boolean removeAll(Collection<?> c)移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。此调用返回后,collection 中将不包含任何与指定 collection 相同的元素。
int[] b = {2,4,6,7,8,9};List list = new ArrayList(Arrays.asList(a));
list.retainAll(Arrays.asList(b)); // list 中的就是交集了.
用qybao(阿宝) 的方法可减少循环次数
2,4,6,7,8,9用两个整型,第一位代表0,第二位1,以此类推.如果上面两组数对应整数应该是:
0101011101........
0010101111........
将这两数进行&操作不就得到交集了.
如果整数集合太大就自己做位域吧.
int[] a = {1,3,5,6,7,9};
int[] b = {2,4,6,7,8,9};
ArrayList list = new ArrayList();
for(int i = 0;i < a.length;i++){
//因为是排好序的,判断当a中一个值大于b中一个值的时候就跳出不用再循环b后面的了
for(int j = 0;j < b.length;j++){
if(a[i] = j[i]){
list.add(a[i]);
}elseif(a[i] < j[i]){
return;//如果a[i]<j[i]就跳出
}else{
j++;//如果a[i]>j[i],就j++继续循环j
}
}
i++;//然后继续循环a
}
这个虽然也循环,但是可以减少循环次数。
private int[] a={0,1,3,3,5,7,9};//去比较
private int[] b={-1,0,2,3,4,5,9,9,10};//被比较
private int[] c=null;
public t(){
c=jiaoJi(a,b);
print(); //打印
}
public int[] jiaoJi(int[] a,int[] b){ //处理交集函数
//确定 中间变量数组 jj 的数组大小,其大小为,a,b两数组较小的数组大小
int size;
if (a.length>b.length){
size=b.length;
}else{
size=a.length;
}
//建立中间变量数组 jj
int [] jj=new int [size];
for (int i=0;i<size;i++){ //赋值
jj[i]=0;
}
int k=0;//记录交集元素插入到jj数组的位置
int e=0;//记录 下一次比较的开始位置
for (int i=0;i<a.length;i++){
for (int j=e;j<b.length;j++){
if (a[i]==b[j]){
jj[k++]=a[i]; //存储交集元素,数组jj 下标 k 加一
e=j+1; //记录 下一次比较的开始位置,既从j后面的元素开始
break;//跳出for循环
}
}
}
int [] zzjj=new int[k]; //根据K建立交集的是数组zzjj
for (int i=0;i<k;i++){
zzjj[i]=jj[i];
}
return zzjj;
} public void print(){//打印
for(int i=0;i<c.length;i++){
System.out.print(c[i]+", ");
}
System.out.println();
} public static void main(String args[]){
new t();
}
}我这个也行的,测试通过
template<class Type>
void union_intersect(vector<Type>& l,vector<Type>& r,vector<Type>& out)
{
sort(l.begin(),l.end());
sort(r.begin(),r.end());
out.clear();
vector<Type>::iterator IL=l.begin();
vector<Type>::iterator IR=r.begin();
while(IL!=l.end()&&IR!=r.end())
{
if(*IL<*IR)
{
while(IL!=l.end()&&*IL<*IR)
++IL;
}
else if(*IR<*IL)
{
while(IR!=r.end()&&*IR<*IL)
++IR;
}
else
{
out.push_back(*IL);
++IL; ++IR;
}
}
}
int[] a = {1,3,5,6,7,9};
int[] b = {2,4,6,7,8,9};
Set s = new HashSet();
for (int i=0, j=0; i<a.length && j<b.length; ) {
if (a[i]==b[j]) {
s.add(""+a[i]);
i++;
j++;
} else if (a[i] > b[j]){
j++;
} else {
i++;
}
}
if (s.size()==0) {
Sytem.out.println("no intersection");
} else {
System.out.println(Arrays.toString(s.toArrays()));
}
list2 size n
public static void getPattern(Object[] strA, Object[] strB) {
// 只要有一个null,就返回null
if (strA == null || strB == null || strA.length == 0
|| strB.length == 0) {
System.out.println("");
return;
}
int lengthA = strA.length;
int lengthB = strB.length;
Object[] longStr = lengthA > lengthB ? strA : strB;
Object[] shortStr = lengthA > lengthB ? strB : strA;
Map<Integer, List<Object[]>> maxSubstrList = new TreeMap<Integer, List<Object[]>>();
for (int length = shortStr.length; length >= 0; length--) {
for (int startIndex = 0; startIndex <= shortStr.length - length; startIndex++) {
Object[] tmpO = new Object[length];
System.arraycopy(shortStr, startIndex, tmpO, 0, length);
String _tmp1 = Arrays.toString(longStr).replaceAll("\\[", "").replaceAll("\\]", "");
String _tmp2 = Arrays.toString(tmpO).replaceAll("\\[", "").replaceAll("\\]", "");
if ((_tmp1.indexOf(_tmp2) > -1) && (Arrays.asList(longStr).containsAll(Arrays.asList(tmpO)))) {
int len = tmpO.length;
List<Object[]> list;
if (maxSubstrList.containsKey(len)) {
list = (List<Object[]>) maxSubstrList.get(len);
} else {
list = new ArrayList<Object[]>();
}
if (!list.contains(tmpO)) {
list.add(tmpO);
}
maxSubstrList.put(len, list);
}
}
}
//找到所有匹配子串
if (maxSubstrList.size() == 0) {
return;
} else {
Iterator<Entry<Integer, List<Object[]>>> it = maxSubstrList
.entrySet().iterator();
Entry<Integer, List<Object[]>> entry = it.next();
while (it.hasNext()) {
entry = it.next();
List<Object[]> maxList = entry.getValue();
for (Object[] str : maxList) {
System.out.println(Arrays.toString(str));
}
}
}
}测试:getPattern(new Integer[]{1,2,4,6,7,8,9},new Integer[]{1,3,4,7,8,9,11,22});
结果:
[1]
[4]
[7]
[8]
[9]
[7, 8]
[8, 9]
[7, 8, 9]
int num = 0;
int[] iaArray = {1,3,5,6,7,9};
int[] ibArray = {2,4,6,7,8,9};
List<Integer> newList = new ArrayList<Integer>();
A:for(Integer a : iaArray){
B:for(int i=bIndex;i<ibArray.length;i++){
num++;
if(a > ibArray[i]){
bIndex = i +1;
continue B;
}
if(a = ibArray[i]){
newList.add(a);
continue A;
}
if(a < ibArray[i]){
continue A;
} }
}循环11次