bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 704. Binary Search

704. Binary Search

Leetcode Solutions

Binary Search

  1. Initialize two pointers left at 0 and right at nums.length - 1.
  2. While left is less than or equal to right: a. Calculate the middle index mid as (left + right) / 2. b. If nums[mid] is equal to target, return mid. c. If nums[mid] is less than target, move the left pointer to mid + 1. d. Else, move the right pointer to mid - 1.
  3. If the target is not found, return -1.
UML Thumbnail

Linear Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...