基本二分、寻找左/右边界二分查找

1. 基本二分查找

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int n = nums.size() - 1;
        int left = 0;
        int right = n;
        while (left <= right) {
            int mid = left + (right - left) / 2; 
            if (nums[mid] > target) // target 在左区间,在[left, middle)中
                right =  mid - 1;
            else if (nums[mid] < target) // target 在右区间,在[middle + 1, right)中
                left = mid + 1;
            else if (nums[mid] == target)// nums[mid] == target
                return mid;
        }
        return -1;
    }  
};

此处while( left <= right ),target在一个左闭右闭区间[left, right],故当nums[mid] > target时,nums[middle]一定不是target, 令right = mid - 1;
若while( left < right ),target在一个左闭右开区间[left, right),故当nums[mid] > target时,令right = mid;

2. 寻找排序数组左侧边界

    int searchleft(vector<int>& nums, int target) {
        int index = -1;
        int left = 0;
        int right = nums.size() - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > target)
                right = mid - 1;
            else if (nums[mid] < target)
                left = mid + 1;
            else if (nums[mid] == target) {
                index = mid;
                right = mid - 1;
            }   
        }
        // 不存在target时, left == nums.size() || nums[left] != target 
        // if ( left == nums.size() || nums[left] != target )
        //     return -1;
        // return left;
        return index;
    }

此处注意nums中不存在target的情况:

1. target比nums中的值都大,那么算法结束 left == len(nums),此时返回-1。
2. target比nums中的值都小,那么算法结束 nums[left] != target,此时返回-1。
也可以用index放在(nums[mid] == target)中避开判断不存在target情况。

3. 寻找排序数组右侧边界

    int searchright(vector<int>& nums, int target) {
        int index = -1;
        int left = 0;
        int right = nums.size() - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > target)
                right = mid - 1;
            else if (nums[mid] < target)
                left = mid + 1;
            else if (nums[mid] == target) {
                index = mid;
                left = mid + 1;
            }   
        }
        // 不存在target时, right == -1 || nums[right] != target 
        // if (right == -1 || nums[right] != target)
        //     return -1;
        // return right;
        return index;
    }

此处注意nums中不存在target的情况:

1. target比nums中的值都大,那么算法结束 nums[right] != target,此时返回-1。
2. target比nums中的值都小,那么算法结束 right == -1,此时返回-1。
也可以用index放在(nums[mid] == target)中避开判断不存在target情况。

最终进行合并可解leetcode 34. 在排序数组中查找元素的第一个和最后一个位置:

class Solution {
public:
    int searchleft(vector<int>& nums, int target) {
        int index = -1;
        int left = 0;
        int right = nums.size() - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > target)
                right = mid - 1;
            else if (nums[mid] < target)
                left = mid + 1;
            else if (nums[mid] == target) {
                index = mid;
                right = mid - 1;
            }   
        }
        // 不存在target时, left == nums.size() || nums[left] != target 
        // if ( left == nums.size() || nums[left] != target )
        //     return -1;
        // return left;
        return index;
    }
    int searchright(vector<int>& nums, int target) {
        int index = -1;
        int left = 0;
        int right = nums.size() - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > target)
                right = mid - 1;
            else if (nums[mid] < target)
                left = mid + 1;
            else if (nums[mid] == target) {
                index = mid;
                left = mid + 1;
            }   
        }
        // 不存在target时, right == -1 || nums[right] != target 
        // if (right == -1 || nums[right] != target)
        //     return -1;
        // return right;
        return index;
    }

    vector<int> searchRange(vector<int>& nums, int target) {
        int left = searchleft(nums, target);
        int right = searchright(nums, target);
        vector<int> ans = {left, right};
        return ans;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值