81 test case passed 8ms Java simple solution


  • 0
    L

    81 / 81 test cases passed.
    Status: Accepted
    Runtime: 8 ms

    public int[] searchRange(int[] nums, int target) {
            int[] notFound = new int[]{-1, -1};
            if(target<nums[0] || target>nums[nums.length-1]) return notFound;
    
            int low=0, high=nums.length, mid;
            while(low<=high) {
                mid= (low+high)>>>1;
                if(nums[mid] == target) {
                    low = high= mid;
                    while (--low>=0 && nums[low] == target);
                    while (++high<nums.length && nums[high] == target);
                    return new int[]{++low, --high};
                } else if(nums[mid] < target) {
                    low = mid+1;
                } else {
                    high = mid -1;
                }
            }
            return notFound;
        }
    

Log in to reply
 

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