C++ solution with lower_bound and upper_bound


  • 0
    Y
    class Solution {
    public:
        vector<int> searchRange(vector<int>& nums, int target) {
            vector<int> result(2,-1);
            std::vector<int>::iterator low=std::lower_bound (nums.begin(), nums.end(), target);
            if(*low != target) return result;
            std::vector<int>::iterator up= std::upper_bound (low, nums.end(), target) -1;
            result[0] = low - nums.begin();
            result[1] = up - nums.begin();
            return result;
        }
    };
    

Log in to reply
 

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