A straight way based on C++ STL library,no more than 15 lines;


  • 0
    E

    class Solution {
    public:
    vector<int> searchRange(vector<int>& nums, int target) {
    pair<vector<int>::iterator,vector<int>::iterator> pr = equal_range(nums.begin(),nums.end(),target);
    vector<int>ans;
    if(pr.first == pr.second){
    ans.push_back(-1);
    ans.push_back(-1);
    return ans;
    }
    ans.push_back(distance(nums.begin(),pr.first));
    ans.push_back(distance(nums.begin(),pr.second-1));
    return ans;
    }
    };


Log in to reply
 

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