标签:style blog ar color sp on div log bs
Implement int sqrt(int x)
.
Compute and return the square root of x.
二分法, 在[0, x/2 + 1]区间内折半查找。
注意mid * mid 可能会溢出。
int sqrt(int x) { unsigned long long low = 0, high = x / 2 + 1, mid = 0; while (low <= high) { mid = (low + high) / 2; if (mid * mid == x) { break; } else if (mid * mid < x) { low = mid + 1; } else { high = mid - 1; } } return mid * mid > x ? mid - 1 : mid; }
标签:style blog ar color sp on div log bs
原文地址:http://www.cnblogs.com/ym65536/p/4149507.html