Straight forward python solution


  • 0
    Y

    Use low, high and middle to check the perfect square

        def isPerfectSquare(self, num):
            """
            :type num: int
            :rtype: bool
            """
            low = 1
            high = num
            result = 1
            while low != high - 1:
                mid = (high + low) / 2
                if mid **2 > num:
                    high = mid
                elif mid **2 < num:
                    result = mid
                    low = mid
                else:
                    return True
            return False
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.