Binary Search

Given a sorted array of integers, search for a target integer. If the target exists, return its index; otherwise, return -1. The algorithm must have O(log n) runtime complexity.

Constraints

  • 1 <= nums.length <= 10^4
  • -10^4 < nums[i], target < 10^4
  • All the integers in nums are unique.
  • nums is sorted in ascending order.
Saving… · 2 lines
class Solution:
def binary_search(self, nums: List[int], target: int) -> int:
Console

Click Run to execute sample tests.