classSolution{publicint[]searchRange(int[] nums,int target){int[] res =newint[2];Arrays.fill(res,-1);if(nums.length ==0|| nums==null){return res;}int left =0;int right = nums.length-1;while(left<right){int mid =(left+right)/2;if(nums[mid]>= target){
right = mid;}else{
left = mid+1;}}if(nums[left]==target){
res[0]= left;}
left =0;
right = nums.length-1;while(left<right){int mid =(left+right+1)/2;if(nums[mid]<= target){
left = mid;}else{
right = mid-1;}}if(nums[left]==target){
res[1]= left;}return res;}}
3- ACM 实现
publicclass findFirstAndLast {publicstaticint[]finFAndL(int[] nums,int target){int[] res =newint[2];Arrays.fill(res,-1);int left =0;int right = nums.length-1;while(left<right){int mid =(left+right)/2;if(nums[mid]>= target){
right = mid;}else{
left = mid+1;}}if(res[left]==target){
res[0]= left;}
left =0;
right = nums.length-1;while(left<right){int mid =(left+right+1)/2;if(nums[mid]<= target){
left = mid;}else{
right = mid-1;}}if(res[left]==target){
res[1]= left;}return res;}publicstaticvoidmain(String[] args){Scanner sc =newScanner(System.in);String input = sc.nextLine();
input = input.substring(1,input.length()-1);String[] parts = input.split(",");int[] nums =newint[parts.length];for(int i =0; i < nums.length;i++){
nums[i]=Integer.parseInt(parts[i]);}System.out.println("输入target");int target = sc.nextInt();int[] res =finFAndL(nums,target);System.out.println("结果是"+res[0]+" "+res[1]);}}