#704

Binary Search

pupil · 365 · lc easy +23 · verified · 60.6% accepted · 13,395 likes · top 59%

Description

Search for target in the sorted integer array nums. Return its index if found, or -1 if it is not present. Your solution must run in O(log n) time.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

Code

1
2
3