use STL in 8 lines


  • 0
    Z
       vector<int> searchRange(vector<int>& nums, int target) {
            vector<int>re{-1,-1};
            if(!binary_search(nums.begin(),nums.end(),target))return re;  //exist or not
            re.clear();
            auto low=lower_bound(nums.begin(),nums.end(),target);     
            re.push_back(low-nums.begin());
            auto up=upper_bound(nums.begin(),nums.end(),target);//low pointe to the first target
            re.push_back(up-nums.begin()-1);  up point to the next of last element equals to target
            return re;
        }
    
    • binary_search O(lgn)
    • upper_bound lower_bound O(lgn)

Log in to reply
 

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