【每日一题】Leetcode - 34. Find First and Last Position of Element in Sorted Array

Question

Leetcode - 34. Find First and Last Position of Element in Sorted Array

Train of thought

Using binary search, if find target, contiguous loop from found position to left or right.

class Solution {

    public int[] searchRange(int[] nums, int target) {
        int tIndex = Arrays.binarySearch(nums, 0, nums.length, target);
        if (tIndex < 0) {
            return new int[] {-1, -1};
        }
        int left = tIndex;
        while (left - 1 >= 0 && nums[left - 1] == target) {
            left--;
        }
        int right = tIndex;
        while (right + 1 < nums.length && nums[right + 1] == target) {
            right++;
        }
        return new int[] {left, right};
    }

}

【每日一题】Leetcode - 34. Find First and Last Position of Element in Sorted Array_第1张图片

Optimize

Only need we to change thought from find the target to find the position of smaller of bigger than targer.

class Solution {

    public int[] searchRange(int[] nums, int target) {
        int start = -1, last = -1;
        //  try to find start
        int left = 0, right = nums.length;
        while (left < right) {
            int mid = (left + right) / 2;
            if (nums[mid] < target) {
                //  ... smaller : cause no result
                if (mid == nums.length - 1) {
                    return new int[] {-1, -1};
                }
                //  smaller, [target], ... bigger : find start
                if (nums[mid + 1] == target) {
                    start = mid + 1;
                    break;
                }
                //  smaller, smaller, ..., [target], ... bigger
                if (nums[mid + 1] < target) {
                    left = mid + 1;
                    continue;
                }
                //  smaller, bigger, .... : cause no result
                //  if (nums[mid + 1] > target) { --- this is default case, so omit
                return new int[] {-1, -1};
            }
            if (nums[mid] == target) {
                start = mid; // maybe there, continue to find
            }
            //  if (nums[mid] >= target) { --- this is default case, so omit
            right = mid;
        }
        // while execute code to here, it represent we find the start, but no find the last
        //  now try to find last
        left = start + 1; right = nums.length;
        while (left < right) {// alway : nums[mid] >= target
            int mid = (left + right) / 2;
            if (nums[mid] == target) {
                last = mid; // maybe there, continue to find
                left = mid + 1;
                continue;
            }
            right = mid;
        }
        if (last != -1) { // find 
            return new int[] {start, last};
        }// no find
        return new int[] {start, start};
    }

}

【每日一题】Leetcode - 34. Find First and Last Position of Element in Sorted Array_第2张图片

你可能感兴趣的:(每日一题,leetcode,算法,java,二分搜索)