MyCode Accepted


  • 0
    H
    class Solution {
    public:
    	double pow(double x, int n) 
    	{
    		double result;
    		if(x==1.00000)
    			return x;
    		else if(x==-1.00000)
    		{
    			 if(n%2==0)
    				 return -x;
    			 else
    			 {
    				 return x;
    			 }
    		}
    		if(n>0)
    		{
    			result=Get_Result(x,n);
    			return result;
    		}
    		else if(n<0)
    		{
    			n=0-n;;
    			result=1.0/Get_Result(x,n);
    			return result;
    		}
    		else
    			return 1.0;
    	}
    	double Get_Result(double x,int n)
    	{
    		if(n==1)
    			return x;
    		else if(n==0)
    			return 1.0;
    		else
    		{
    			if(n%2==0)
    			{
    				double tt=Get_Result(x,n/2);
    				return tt*tt;
    			}
    			else
    			{
    				double tt=Get_Result(x,(n-1)/2);
    				return tt*tt*x;
    			}
    		}
    	}
    };

Log in to reply
 

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