Short easy 16ms C++ solution using multiset


  • 0
    class Solution {
    public:
        vector<int> searchRange(vector<int>& nums, int target) {
            multiset<int> m;
            vector<int> res;
            for (int i=0; i<nums.size(); i++) {
                if (target==nums[i]) m.insert(i);
            }
            if (!m.empty()) {
                res.push_back(*m.begin());
                res.push_back(*m.rbegin());
                return res;
            }
            return {-1, -1};
        }
    };
    

    // Not the best method but another idea, notice that *m.end() won't give you the last element but next to the last element.


Log in to reply
 

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