Power(x,n)
Anonymous
java
03/11/2024 3:03 AM
712 B
31
Indexable
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);
}
}Editor is loading...
Leave a Comment