C solution: 0ms & 95%


  • 0
    I
    /*
    ** https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
    */
    static int  ft_sqrt(int n)
    {
        int res;
        int one;
        
        for (one = 1 << 30; one > n; one >>= 2)
            continue ;
        for (res = 0; 0 != one; one >>= 2)
        {
            if (n >= res + one)
            {
                n -= res + one;
                res += one << 1;
            }
            res >>= 1;
        }
        return (res);
    }
    
    int         *constructRectangle(int n, int *ret)
    {
        int *tupple;
        
        *ret = 2;
        tupple = calloc(*ret, sizeof(int));
        for (int l = ft_sqrt(n); 0 != l; --l)
        {
            if (0 == (n % l))
            {
                tupple[0] = n / l;
                tupple[1] = l;
                break ;
            }
        }
        return (tupple);
    }
    

Log in to reply
 

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