讨论/《二分查找》 - x 的平方根/
《二分查找》 - x 的平方根
int mySqrt(int x){
    if(x < 2)
        return x;
    long long left = 1,right = x/2,mid;
    while(left<=right)
    {
        mid = left + (right - left) / 2;
        if(mid*mid == x)
            return mid;
        else if(mid*mid<x)
            left = mid+1;
        else
            right = mid-1;
    }
    if(mid*mid>x)
        mid--;
    return mid;
}
展开全部 11 讨论