My simple C++ solution


  • 0
    B
    class Solution {
    public:
    double myPow(double x, int n) {
    	if (n == 0)
    	{
    		return 1.0;
    	}
    	if (n < 0 && n != INT_MIN)
    	{
    		n = -n;
    		x = 1 / x;
    	}
    	else if(n == INT_MIN)
    	{
    		x = 1 / x;
    		n = -(n + 1);
    		return n % 2 == 0 ? x*myPow(x*x, n / 2) : x*x*myPow(x*x, n / 2);
    	}
    
    	return n % 2 == 0 ? myPow(x*x, n / 2) : x*myPow(x*x, n / 2);
    }
    };
    

Log in to reply
 

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