num, write a function which returns True if num

Note: Do not use any built-in library function such as ​​sqrt​​.

Example 1:


Input: 16 Returns: True


Example 2:


注意 mid 和 tmp都必须是long long 防止tmp溢出!


class Solution {
public:
bool isPerfectSquare(int num) {
int left=0,right=num/2+1;
while(left<=right){
long long mid=left+(right-left)/2;
long long tmp=mid*mid;
if(tmp==num){
return true;
}else if(tmp>num){
right=mid-1;
}else{
left=mid+1;
}
}
return false;
}
};