import java.util.*; public class Main{ public static void main(String[] args){ int[] array = new int[]{1,2,3,3,3,3,4}; Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] res = minAndmax(array,n); System.out.println(res[0]+","+res[1]); } public static int[] minAndmax(int[] array,int n){ int[] res = new int[]{-1,-1}; int left = 0; int right = array.length-1; while(left<right){ int mid = left+(right-left)/2; if(n==array[mid]){ left = mid; }else if(n<array[mid]){ right = mid-1; }else { left = mid+1; } if(left==right-1){ res[1]=left; break; } if(left==right && n==array[left]){ res[0]=left; res[1]=right; return res; } } right = left; left = 0; while(left<right){ int mid = left+(right-left)/2; if(n==array[mid]){ right=mid; }else if(n>array[mid]){ left=mid+1; }else { right=mid-1; } } if(n==array[left]){ res[0]=left; } return res; } }
全部评论
(1) 回帖