Implement int sqrt(int x)
.
Compute and return the square root of x.
Subscribe to see which companies asked this question
分析:
数学方法解答。很奇怪我的不能用平常算法写完。参照别的。
代码:
class Solution {
public:
int mySqrt(int x) {
if(x==0) return 0;
long long res=x;
while((res*res)>x)
{
res=(res+x/res)/2;
}
return res;
}
};