#34
Find First and Last Position of Element in Sorted Array
specialist · 890 · lc medium +31 · verified · 48.4% accepted · 23,161 likes · top 34%
Description
In non-decreasing sorted integer array nums, find the leftmost and rightmost indices of target. Return [-1, -1] if target is not found. The solution must run in O(log n) time.
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
Example 3:
Input: nums = [], target = 0
Output: [-1,-1]
Code
1
2
3