Leetcode: Pow(x, n) in C++
Implement pow(x, n).
Solution:
double pow(double x, int n) {
if(n==1)
return x;
else if(n==0)
return 1;
int absn = abs(n);
double k = pow(x,absn/2);
double result = k*k*pow(x,absn%2);
if(n<0)
result = 1.000/result;
return result;
}
No comments:
Post a Comment