Given a positive integernum, write a function which returns True ifnumis a perfect square else False.
class Solution {
public boolean isPerfectSquare(int num) {
if (num < 1) {
return false;
}
long start = 1, end = num, mid = 0;
while (start + 1 < end) {
mid = start + (end - start) / 2;
if (mid * mid == num) {
return true;
}
else if (mid * mid < num) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
if (start * start == num || end * end == num) {
return true;
}
else {
return false;
}
}
}