Sqrt(x)


Implement int sqrt(int x).

Compute and return the square root of x.


Solution

1
2
3
4
5
6
7
8
9
public class Solution {
public int mySqrt(int x) {
long r = x;
while(r*r > x) {
r = (r+x/r)/2;
}
return (int)r;
}
}