C++ O(lgn) using STL functions

  • 0
    class Solution {
        vector<int> searchRange(vector<int>& a, int target) {
            auto it = lower_bound(a.begin(),a.end(), target);
            auto it2 = upper_bound(a.begin(),a.end(), target);
            if (it==a.end() or *it != target) return {-1,-1};
            if (it2 != a.end()) {
                it2 = prev(it2);
                return {distance(a.begin(),it), distance(a.begin(), it2)};
            } else if (it != a.end())
                return {distance(a.begin(),it), a.size()-1};
                return {-1,-1};

Log in to reply

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