MyPow c solution 0 ms?


  • 0
    Y
    double myPow(double x, int n) {
    if (n == 0) return 1;
    if (n == 1) return x;
    if (n == -1) return 1/x;
    double val;
    if (n > 0){
    	val = myPow(x, n / 2);
    	if (n % 2 == 1){
    		return val*val*x;
    	}
    	else
    		return val*val;
    }
    else{
    	val = 1 / myPow(x, -n / 2);
    	if (n % 2 == -1){
    		return val *val / x;
    	}
    	else{
    		return val * val;
    	}
    }
    

    }


Log in to reply
 

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