19. Binary Search
MediumSearching~15 min
Given a sorted array of distinct numbers and a target, return the index of the target, or -1 if it is not present.
Your solution should be noticeably faster than scanning every element.
Examples
Example 1
- Input:
- nums = [-1, 0, 3, 5, 9, 12], target = 9
- Output:
- 4
Example 2
- Input:
- nums = [-1, 0, 3, 5, 9, 12], target = 2
- Output:
- -1
Constraints
0 <= nums.length <= 10000nums is sorted ascending and all values are distinct.