题目链接:https://leetcode.com/problems/valid-perfect-square/description/
找有效的完全平方根。不能使用sqrt。
为了防止溢出,需要开long long int
bool isPerfectSquare(int num) {
long long int l = 0,r = num;
long long int mid,total;
int ans = -1;
if(num == 1) ans = 1;
while(l < r){
mid = l + (r - l) / 2;
total = mid * mid;
if(total < num){
l = mid + 1;
}else if(total > num){
r = mid;
}else{
ans = 1;
break;
}
}
if(ans == 1){
return true;
}else{
return false;
}
}