Python binary search

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

Log in to reply

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