[leetcode] sqrt(x)
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 惭愧,看了tag才知道需要用binary search。 二分查找,需要注意把x改为long long以免数据溢出。 另外,一个正数的平方根只能在[1, x / 2 + 1)之间变动。 class Solution { public: int sqrt(int x) { if(x < 2) return x; return _sqrt(x, 1, x / 2 + 1); } //[start, end) int _sqrt(int x, […]