[C++]The Official Answer to this problem MS VS 4lines


  • 0

    It's clean and no bugs.

    double myPow(double x, int n)
    {
    	unsigned int m = (n > 0)?n:-n;
    
    	for(double res = double(1);; x *= x)
    	{
    		if((m & 1) != 0)
    		{
    			res *= x;
    		}
    		if((m >>= 1) == 0)
    		{
    			return (n < 0?double(1) / res:res);
    		}
    	}
    
    }
    

Log in to reply
 

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