Power(x,n)

mail@pastecode.io avatar
unknown
java
a month ago
534 B
13
Indexable
Never
class Solution {

    public double myPow(double x, int n) {
        boolean hasNegativePower=false;

        if(n<0)
        {
            hasNegativePower=true;
            n=Math.abs(n);

        }

        double  result=helperMyPow(x,n);

        if(hasNegativePower){

            return 1/result;
        }

        return result;
        
        
    }
    
    public double helperMyPow(double x,int n)
    {

        if(n==0)
        return 1;

        return x*helperMyPow(x,n-1);

    }
}
Leave a Comment