Skip to content
LC-0367 Easy LeetCode

367. Valid Perfect Square

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 44% Topics: Math, Binary Search
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(logn)
# Space: O(1)

class Solution(object):
    def isPerfectSquare(self, num):
        """
        :type num: int
        :rtype: bool
        """
        left, right = 1, num
        while left <= right:
            mid = left + (right - left) // 2
            if mid >= num / mid:
                right = mid - 1
            else:
                left = mid + 1
        return left == num / left and num % left == 0

Solution from kamyu104/LeetCode-Solutions · MIT