Skip to content

Instantly share code, notes, and snippets.

@PeculiarE
Created March 5, 2026 20:27
Show Gist options
  • Select an option

  • Save PeculiarE/3bd69069653027917dc77b760dbc8e94 to your computer and use it in GitHub Desktop.

Select an option

Save PeculiarE/3bd69069653027917dc77b760dbc8e94 to your computer and use it in GitHub Desktop.
Valid Perfect Square - LeetCode - Day 64

Question

Intuition

Approach

Complexity

  • Time complexity: $$O(log \ n)$$

  • Space complexity: $$O(1)$$

Code

class Solution:
    def isPerfectSquare(self, num: int) -> bool:
        low, high = 0, num
        while (low <= high):
            mid = (low + high) // 2
            if mid * mid == num:
                return True
            elif mid * mid >= num:
                high = mid - 1
            else:
                low = mid + 1
        return False

Result

Screenshot 2026-03-05 at 20 21 53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment