leetcoe-69-x的平方根-C语言

/*
 * 算法思想:
 * 二分法
 */

int mySqrt(int x){
    unsigned long long i=1, tmp, j=x, mid;
    
    while(i<=j) {
        mid = i+(j-i)/2;
        
        tmp = mid * mid;
        if(tmp > x) {
            j = mid -1;
        }else if(tmp < x) {
            i = mid + 1;
        }else {
            break;
        }
    }
    
    return mid*mid > x ? mid-1 : mid;
}


你可能感兴趣的:(LeetCode)