intFindLeft(vector<int>& nums, int target){ int l = 0; int r = nums.size() - 1; int left = -1; while (l <= r){ int mid = l + (r - l) / 2; if (nums[mid] == target){ left = mid; r = mid - 1; }elseif (nums[mid] < target){ l = mid + 1; }else{ r = mid - 1; } } return left; }
intFindRight(vector<int>& nums, int target){ int l = 0; int r = nums.size() - 1; int right = -1; while (l <= r){ int mid = l + (r - l) / 2; if (nums[mid] == target){ right = mid; l = mid + 1; }elseif (nums[mid] < target){ l = mid + 1; }else{ r = mid - 1; } } return right; }
vector<int> searchRange(vector<int>& nums, int target){ vector<int> ans; int l = FindLeft(nums, target); int r = FindRight(nums, target); ans.push_back(l); ans.push_back(r); return ans; }