对于诸如此类的问题,比如POW(x,n)采用分治的方法来规避大数据的问题


  • 0
    T

    对于大数据的问题采用分治的方法求解
    double power(double x, int n)
    {
    if(n == 0)
    {
    return 1.0;
    }
    double v = power(x, n / 2);
    if(n % 2 == 0)
    {
    return v * v;
    }
    else
    {
    return v * v * x;
    }
    }

    double myPow(double x, int n)
    {
    if(n < 0)
    {
    return 1.0 / power(x, -n);
    }
    else
    {
    return power(x, n);
    }
    }


Log in to reply
 

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