21. Binary Search

nums is sorted in ascending order with distinct values. Return the index of target, or -1 if it is not there. Your algorithm must run in O(log n).

Example 1

Input: nums = [-3,0,4,8,15], target = 8
Output: 3

Example 2

Input: nums = [-3,0,4,8,15], target = 5
Output: -1
Hint 1

Compare with the middle element and throw away the half that cannot contain the target.

Binary Search