Leetcode Problem 374. Guess Number Higher or Lower

374. Guess Number Higher or Lower

Leetcode Solutions

Approach: Using Binary Search

  1. Initialize two pointers left and right to 1 and n respectively.
  2. While left is less than or equal to right: a. Calculate the middle of the current range as mid = left + (right - left) / 2. b. Make a guess using mid and store the result of guess(mid). c. If the result is 0, return mid as the picked number. d. If the result is -1, adjust the right pointer to mid - 1. e. If the result is 1, adjust the left pointer to mid + 1.
  3. If the loop ends without returning, the picked number was not found (which should not happen in this problem).
UML Thumbnail

Approach: Brute Force

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...