样例输入
4 4
5 1 7 3
2 3 4 1
样例输出
1 3
import java.util.Arrays;
import java.util.Scanner;
public class 开花 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
long [] a=new long[100005];
long [] b=new long[100005];
// int [] b=new int[m];
for (int i = 0; i <n ; i++) {
a[i]=sc.nextInt();
}
for (int i = 0; i <m ; i++) {
b[i]=sc.nextInt();
}
Arrays.sort(b,0,m);
for (int i = 0; i <n ; i++) {
int x=Arrays.binarySearch(b,0,m,a[i]);
if (x>=0){
System.out.print(a[i]+" ");
}
}
}
}
考点:
二分法:对于javaArrays.binarySearch,时间的优化
来源:CSDN
作者:小白学习笔记
链接:https://blog.csdn.net/weixin_43673156/article/details/104568997