Last Position of Target
Find the last position of a target number in a sorted array. Return -1 if target does not exist.
Example
Given[1, 2, 2, 4, 5, 5]
.
For target =2
, return 2.
For target =5
, return 5.
For target =6
, return -1.
当找到与target相等的值时,继续向右寻找(start=mid),因为题目要求寻找最后一个index
循环结束判断时,需要先判断A[end],然后再判断A[start]
Last updated
Was this helpful?