Find First and Last Position of Element in Sorted Array
找有重复数字的左边界:
mid >= target end= mid ,不断缩小右窗口,
最后check start和end的时候,先check start,因为要取左窗口
找有重复数字的右边界:
mid <= target start = mid 不断缩小左窗口
最后check start和end的时候,先check end,因为要取右窗口
Given an array of integers nums
sorted in ascending order, find the starting and ending position of a given target
value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1]
.
Example 1:
Example 2:
Last updated