Binary search
Easybinary searcharray
nums is sorted ascending with distinct values. Return the index of target, or -1 if it's absent, in O(log n).
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
- 1 ≤ nums.length ≤ 10⁴
- nums is sorted and distinct
Submit checks your solution against 8 tests, including hidden ones.
/**
* @param {int[]} nums
* @param {int} target
* @return {int}
*/
function search(nums, target) {
}