Carmack'sqrt time O(1) and space O(1)


  • 0
    H
    int mySqrt(int number) {
        long i; 
        float x, y; 
        const float f = 1.5F; 
        x = number * 0.5F; 
        y  = number; 
        i  = * ( long * ) &y; 
        i  = 0x5f3759df - ( i >> 1 ); 
        y  = * ( float * ) &i; 
        y  = y * ( f - ( x * y * y ) ); 
        y  = y * ( f - ( x * y * y ) ); 
        long xx = number * y + 1;
        return xx*xx <= number ? xx : xx-1;
    }

Log in to reply
 

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