Power(x,n)
unknown
java
2 years ago
534 B
18
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