C++


  • 0
    B
    class Solution {
    public:
        vector<int> searchRange(vector<int>& nums, int target) {
            int first;
            if (nums.size() < 1) return vector<int>{-1,-1};
            int i = 0; int j = nums.size()-1;
            while(i < j) {
                int mid = (i+j)/2;
                if (target > nums[mid]) i = mid+1;
                else j = mid;
            }
            if (nums[i] != target) return vector<int>{-1,-1};
            first = i;
            j = nums.size()-1;
            while(i < j) {
                int mid = (i+j)/2;
                if (target < nums[mid]) j = mid-1;
                else i = mid+1;
            }
            if (nums[i] == target) return vector<int>{first,i};
            else if (nums[i-1] == target) return vector<int>{first,i-1};
            else if (nums[i+1] == target) return vector<int>{first,i+1};
            else return vector<int>{-1,-1};
        }
    };

Log in to reply
 

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