Basic Binary Search

  • 0
    class Solution(object):
        def isPerfectSquare(self, num):
            :type num: int
            :rtype: bool
            l, r = 1, num
            while l <= r:
                m = (l+r) /2
                if m**2 < num:
                    l = m+1
                elif m**2 > num:
                    r = m-1
                    return True
            return False

Log in to reply

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