My simple c++ solution


  • 0
    K
    class Solution {
    public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int lower=-1, upper=-1, start=0, end=nums.size()-1;
        if(end == -1)return {lower, upper};
        if(end==0 && nums[0]!=target)return {lower, upper};
        while(start < end){
            int mid=start + (end-start)/2;
            if(nums[mid] < target)start=mid+1;
            else end=mid;
        }
        lower=start;
        if(nums[lower]!=target)return {-1,-1};
        upper=start;
        start++;
        end=nums.size()-1;
        while(start <=end){
            if(nums[start]==target)upper++;
            else break;
            start++;
        }
        
        return {lower, upper};
    }
    

    };


  • 0
    Z
    This post is deleted!

Log in to reply
 

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