java怎么过不去.前两个re,第三个wrong,re tle
查看原帖
java怎么过不去.前两个re,第三个wrong,re tle
1209691
Wwz666楼主2024/11/7 14:46
import java.util.*;
public class Main{
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int q = sc.nextInt();
        int[] nums = new int[n];
        for(int i = 0;i<n;i++){
            nums[i] = sc.nextInt();
        }
        sc.nextLine();
        String[] s = sc.nextLine().split(" ");
        int i = 0;
        while(q-->0){
            int target = Integer.parseInt(s[i]);
            int l = 0;
            int r = n-1;
            while(l<=r){
                int mid = l+(r-l)/2;
                if(nums[mid]>=target){
                    r = mid-1;
                }else{
                    l = mid+1;
                }
            }
            i++;
            if(nums[l]==target) System.out.print(l+1+" ");
            else System.out.print("-1 ");
        }
    }
}
2024/11/7 14:46
加载中...