0ms java solution, use two binary search


  • 2
    I

    The solution has two binary search to find the left and right boundary

    public int[] searchRange(int[] nums, int target) {
        
        int[] result = {-1,-1};
        if(nums == null || nums.length == 0){
            return result;
        }
        // Binary search to find the left boundary
        int start = 0;
        int end = nums.length-1;
        while(start <= end){
            int mid = start + (end-start)/2;
            if(target <= nums[mid] ){
                if(target == nums[mid])
                result[0] = mid;
                end = mid-1;
            }else{
                start = mid +1;
            }
        }
       // Binary search to find the right boundary
        end = nums.length-1;
        while(start <= end){
            int mid = start + (end-start)/2;
            if(target >= nums[mid]){
                if(target == nums[mid])
                result[1] = mid;
                start = mid+1;
            }else{
                end = mid-1;
            }
        }
        return result;
    }

Log in to reply
 

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