my neat code, O(log n)


  • 0
    H
    public int mySqrt(int x) {
        int root = 0;
        for (int i = 1; x > 0; i += 2, root++)
            x -= i;
        return (x == 0) ? root : root - 1;
    }
    

Log in to reply
 

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