Valid Perfecr Square
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt.
Example
Input: 16
Output: true
Solution
class Solution:
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
if num==1:
return True
left = 0
right = num
while left+1!=right:
mid = (left+right)//2
if num>mid*mid:
left = mid
elif num<mid*mid:
right = mid
else:
return True
return False