题目链接:https://leetcode-cn.com/problems/valid-perfect-square/
难度:简单
描述:
给定一个 正整数 num
,编写一个函数,如果 num
是一个完全平方数,则返回 true
,否则返回 false
。
进阶:不要 使用任何内置的库函数,如 sqrt
。
题解
class Solution:
def isPerfectSquare(self, num: int) -> bool:
left, right = 1, num
while left <= right:
mid = (left + right) // 2
if mid * mid > num:
right = mid - 1
elif mid * mid < num:
left = mid + 1
else:
return True
return False