Simple java code o(log n)


  • 0
    N

    public double power(double x, int n) {
    if (n == 0)
    return 1;
    if (n == 1)
    return x;
    double pow = power(x, n / 2);
    if (n % 2 == 0)
    return pow * pow;
    else
    return x * pow * pow;
    }

    public double myPow(double x, int n) {
    	if (n < 0)
    		return 1 / power(x, n * -1);
    	return power(x, n);
    }

Log in to reply
 

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