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

这篇文章介绍了一种使用二分查找优化的方法来解决LeetCode第34题,即在已排序的数组中找到目标元素的第一个和最后一个出现的位置。初始解决方案是找到目标元素后向两侧扩展,优化后的思路是在查找过程中同时寻找目标元素的边界。

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};
    }

}

在这里插入图片描述

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};
    }

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值