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

Leetcode Problem 367. Valid Perfect Square

367. Valid Perfect Square

Leetcode Solutions

Approach: Binary Search

  1. If num is less than 2, return True since 0 and 1 are perfect squares.
  2. Set the left boundary to 2 and the right boundary to num / 2.
  3. While the left boundary is less than or equal to the right boundary: a. Take the middle element as the guess (x = (left + right) / 2). b. Calculate guess_squared (x * x). c. If guess_squared equals num, return True. d. If guess_squared is greater than num, move the right boundary to x - 1. e. If guess_squared is less than num, move the left boundary to x + 1.
  4. If the loop ends, return False as num is not a perfect square.
UML Thumbnail

Approach: Newton's Method

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...