Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

Solution:

public class Solution {
    public int mySqrt(int x) {
        long r = x;
        while(r*r>x) r = (r+x/r)>>1;
        return (int)r;
    }
}
comments powered by Disqus