题目
Given a positive integer num, write a function which returns True if num is a perfect square else False.
答案
class Solution {
public boolean isPerfectSquare(int num) {
long l = 0, r = num+1;
while(l < r) {
long m = (l + r) / 2;
long prod = m*m;
if(prod == num) {
return true;
}
if(prod < num) {
l = m + 1;
}
else {
r = m;
}
}
return false;
}
}