My 12ms ,logN c++ solution.


  • 0
    M
    class Solution {
    public:
        vector<int> searchRange(vector<int>& nums, int target) {
            int l=0,r=nums.size();
            vector<int> ans(2,-1);
            while(l<r){
                int mid=(l+r)/2;
                if(nums[mid]==target&&(mid==0||nums[mid-1]<nums[mid])){
                    ans[0]=mid;break;
                }
                else if(nums[mid]<target) l=mid+1;
                else r=mid;
            }
            l=0;r=nums.size();
            while(l<r){
                int mid=(l+r)/2;
                if(nums[mid]==target&&(mid==nums.size()-1||nums[mid+1]>nums[mid])){
                    ans[1]=mid;break;
                }
                else if(nums[mid]<=target) l=mid+1;
                else r=mid;
            }
            return ans;
        }
    };

Log in to reply
 

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