Last updated
Last updated
class Solution:
def isPerfectSquare(self, num: int) -> bool:
if num == 1:
return True
left = 1
right = num // 2
while left <= right:
mid = left + (right - left) // 2
square = mid * mid
if square == num:
return True
elif square > num:
right = mid - 1
elif square < num:
left = mid + 1
return False
class Solution {
public boolean isPerfectSquare(int num) {
if (num == 1) {
return true;
}
long left = 1;
long right = num / 2;
long mid, square;
while(left <= right) {
mid = left + (right - left) / 2;
square = mid * mid;
if (square == num) {
return true;
} else if (square > num) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return false;
}
}