left
to 0 and right
to the length of letters
minus 1.left
is less than or equal to right
:
a. Calculate the middle index mid
as the average of left
and right
.
b. If letters[mid]
is less than or equal to target
, move the left
pointer to mid + 1
.
c. Otherwise, move the right
pointer to mid - 1
.left
will be the index of the smallest character greater than target
or the length of letters
if no such character exists.left
is equal to the length of letters
, return letters[0]
.letters[left]
.