16lines,8ms,Java BinarySearch


  • 0
    X
    public class Solution {
      public static int[] searchRange(int[] array ,int target){
            int[] index={-1,-1};
            if (array.length == 0){
                return index;
            }
    
            int mid= Arrays.binarySearch(array,target);
            if (!(mid >=0 && mid <=array.length-1)){
                return index;
            }
    
            //扩充你的边界
            int right=mid;
            while(right <array.length && array[right] == target ){
                right ++;
            }
            int left=mid;
            while (left >=0 && array[left] ==target){
                left--;
            }
            index[0]=left+1;
            index[1]=right-1;
            return index;
    
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.