Search for a Range


  • 0
    L

    Hello, can anyone check the code below if there is any problem. Thanks.

    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> result;
        int len = (int) nums.size();
        int start = 0, end = len-1;
        while (start <= end) {
            int mid = (start + end)/2;
            if (nums[mid] == target) {
                while (nums[--mid] == target);
                start = ++mid;
                while (nums[++mid] == target);
                end = mid-1;
                result.push_back(start);
                result.push_back(end);
                return result;
            }
            if (nums[mid] < target)  start = mid+1;
            if (nums[mid] > target)   end = mid-1;
        }
        result.push_back(-1);
        result.push_back(-1);
        return result;
     }

Log in to reply
 

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