8 lines python binary search beat 92.17%


  • 0
    B
    class Solution(object):
        def isPerfectSquare(self, num):
            i,j = 1,num/2+1
            while  i <= j:
                mid = (i+j) >> 1
                if mid*mid >= num:
                    j = mid-1
                else:
                    i = mid +1
            return i*i == num
    

Log in to reply
 

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