刚学java不会优化,求教。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a[] = new int[1000001];
int n,m;
n = sc.nextInt();
m = sc.nextInt();
for(int i = 0;i < n;i ++) a[i] = sc.nextInt();
while(m > 0)
{
int x;
x = sc.nextInt();
int l = 0,r = n-1;
while(l < r)
{
int mid = (l+r) / 2;
if(a[mid] >= x) r = mid;
else l = mid + 1;
}
if(a[l] != x) System.out.print("-1 ");
else System.out.print((l+1) + " ");
m --;
}
}
}