Mr_sqw
import java.util.Arrays.*
public class PrintAB
{
int A[],int B[];
public void printAB(A,B)
{
if(A.length<1 ||B.length<1)
return 0;
int i,j;
Arrays.sort(A);
Arrays.sort(B);
while(i<A.length&&j<B.length)
{
if(A[i]<=B[j]
System.out.print(A[i++]+" ");
else System.out.print(B[j++]+" "); }
if(i==A.length)
while(i<A.length) System.out.print(A[i++]+" ");
if(i==B.length) while(i<B.length) System.out.print(B[j++]+" " ); } }
Python写法 思路就是两个数组排序,然后用两个下标分别指向两个数组,比较大小, 相同则存储,小的则下标加一,直到一个数组遍历完毕结束,算法结束。 def printarray(array1, array2): len1 = len(array1) len2 = len(array2) array1.sort() array2.sort() i = 0 j = 0 array = [] while i != len1 - 1 or j != len2 - 1: if array1[i] == array2[j]: array.append(array1[i]) i += 1 j += 1 elif array1[i] < array2[j]: i += 1 else: j += 1 print array
import java.util.Collections;
import java.util.List;
/**
* 给定2个大小分别为n, m的整数集合,分别存放在两个数组中 int A[n], B[m],输出两个集合的交集。
*/
public class Question1Answer {
public static void main(String[] args) {
int[] a = {5,68,44,33,100,66};
int [] b = {68,33,2,40,100};
StringBuffer sb = new StringBuffer();
for(int i=0;i<a.length;i++){
for(int j=0;j<b.length;j++){
if( a[i] == b[j] ){
sb.append(a[i]).append(",");
break;
}
}
}
System.out.print("a,b二个集合的交集是:");
System.out.println("["+sb.substring(0,sb.length()-1)+"]");
}
} print 'got it',e
import java.util*;
public class Main{
public ArrayList<Integer> findsame(int[] A,int[] B){
int n = A.length;
int m = B.length;
boolean[] boo = new boolean[m];
ArrayList<Integer> al = new ArrayList<Integer>();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++){
if(A[i]==B[j] && !boo[j]){
boo[j] = true;
al.add(A[i]);
break;
}
}
}
return al;
}
} int* merge(int A[],int B[])
{
int * out,m_pA,m_pB;
m_pA = A;
m_pB = B;
int i = 0;
while(m_pA)
{
while(m_pB)
{
if(*m_pB==*m_pA)
{
out[i]=*m_pA;
i++;
}
m_pB++;
}
m_pA++;
}
return out;
} class solution {
public: vector<int> f(int *a, int *b, int n, int m) { vector<int>temp, q; for (int i = 0; i < n; i++) { temp.push_back(a[i]); } for (int i = 0; i < m; i++) { vector<int>::iterator t = find(temp.begin(), temp.end(), b[i]); if (t != temp.end()) q.push_back(b[i]); } return q; }
};
import java.util.ArrayList; /** * Created by Administrator on 2017/3/4. */ public class test1 { public static void main(String[] args) { int[] A = {1,2,3,6,3,6,1,2,7,9,0,54}; int[] B = {1,2,3,7,3,2,7,3,6,1,2,7,9,0,54}; ArrayList<Integer> result = cross(A,B); System.out.println(result.toString()); } public static ArrayList<Integer> cross(int[] A, int[] B){ ArrayList<Integer> C=new ArrayList<>(); ArrayList<Integer> arrayList=new ArrayList<>(); for (int a : A) { arrayList.add(a); } for (int b:B){ if (arrayList.contains(b)){ arrayList.remove(arrayList.indexOf(b)); C.add(b); } } return C; } }