- Sqrt(x)
Implement int sqrt(int x).
Compute and return the square root of x.
Example
Example 1:
Input: 0
Output: 0
Example 2:
Input: 3
Output: 1
Explanation:
return the largest integer y that y*y <= x.
Example 3:
Input: 4
Output: 2
Challenge
O(log(x))
解法1:
思路:binary search。但是并不简单,机关很多。
- mid和midSquare都必须用long long,不然当x很大时mid*mid溢出。为什么mid也要是long long呢? 因为
long long midSquare = mid*mid;
这里如果mid是int的话,mid*mid就已经溢出了,所以midSquare是long long 也没有用。 - 这题是返回bottom boundary,即mid * mid <= x。
- 该模板while()结束后如果没有返回mid,最后start,mid和end的关系是start<=mid<=end,他们各自之间最多差1。
那么我们是不是只看start就可以了呢?答案是错。因为endend可能等于x,而startstart<x,这种情况我们必须选end。所以我们先看end*end是不是已经等于x,是的话返回end,否则返回start。
代码如下:
class Solution {
public:
/**
* @param x: An integer
* @return: The sqrt of x
*/
int sqrt(int x) {
int start = 0, end = x;
while(start + 1 < end) {
long long mid = start + (end - start) / 2;
long long midSquare = mid * mid;
if (midSquare < x) {
start = mid;
} else if (midSquare > x) {
end = mid;
} else {
return mid;
}
}
if (end * end <= x) return end;
return start;
}
};
解法2:牛顿迭代法
因为牛顿迭代法是用来求方程F(x)=0的根。这里是不是F(x)就等于sqrt(x)呢?不对。
我们应该把x当成常量,然后让x = t * t,t为自变量,来求F(t)=t*t-x=0的根。
class Solution {
public:
/**
* @param x: An integer
* @return: The sqrt of x
*/
int sqrt(int x) {
// Let x = t * t, t is varibale
// Then return the root of t * t - x = 0
double t = x / 2.0; //这里令t=x也可以
while(fabs(t * t - x) > 1e-6) {
t -= (t * t - x) / (2 * t); //new t = t - F(t)/F'(t)
}
return t;
}
};
解法3:著名的某个游戏中使用的sqrt()代码,我感觉原理是基于级数展开。
https://web.archive.org/web/20090224214314/http://www.geometrictools.com/Documentation/FastInverseSqrt.pdf
class Solution {
public:
/**
* @param x: An integer
* @return: The sqrt of x
*/
int sqrt(int x) {
// write your code here
float y=x;
long i=* ( long * ) &y;
i = 0x5f375a86 - ( i >> 1 );
y = 1/ * ( float * ) &i;
y = (y+x/y)/2;
y = (y+x/y)/2;
return abs(y);
}
};