public boolean isPerfectSquare(int num) {
int left = 1;
int right = num;
while(left <= right){
int mid = left + ((right- left)>>1);
int res = num/mid;
if(res == mid && num%res == 0)//注意num%res == 0
return true;
else if(res > mid)
left = mid + 1;
else
right = mid -1;
}
return false;
}
注意一下两个整数相除之后是向下取整的,这就可能res的值偏小,所以最后确定的时候需要先num%res==0才能确定答案