findBound
that performs a binary search to find the first or last occurrence of the target.begin
and end
, to keep track of the current search range.begin
is less than or equal to end
, calculate the middle index mid
.nums[mid]
is equal to the target, check if it's the first or last occurrence based on the isFirst
flag.mid
is 0 or nums[mid - 1]
is not the target, return mid
.mid
is the last index or nums[mid + 1]
is not the target, return mid
.begin
or end
pointers based on the comparison between nums[mid]
and the target.searchRange
, call findBound
twice with isFirst
set to true
and false
to find the first and last positions, respectively.[-1, -1]
as the target is not present.