题目:
Implement int sqrt(int x)
.
Compute and return the square root of x.
分析:
。。以后写算法题的时候打算在最后加上题目类型。
代码:
class Solution {
public:
int mySqrt(int x) {
if(x<2)return x;
long q=x;
while(q*q>x){
q=(q+x/q)/2;
}
return q;
}
};