Python Log(N) -- Using Binary search


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

Log in to reply
 

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