My C 5ms solution using something like binary search


  • 3
    P
    int mySqrt(int x) {
        if (x<=1) return x;
        long long lb=1, rb=x, md;
        while(rb-lb>1)
        {
            md = (rb+lb)/2;
            if (md*md==x)
                return md;
            if (md*md>x)
                rb=md;
            else
                lb=md;
        }
        if (rb*rb==x)
            return rb;
        return lb;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.