class Solution {
//divide-and-conquer
//classic
public:
double pow(double x, int n) {
if (n == 0) return 1.0;
// Compute x^{n/2} and store the result into a temporary
// variable to avoid unnecessary computing
double half = pow(x, n / 2);
if (n % 2 == 0)
return half * half;
else if (n > 0)
return half * half * x;
else
return half * half / x;
}
};
second time
class Solution {
public:
double pow(double x, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n == 0) return 1;
else if(n > 0)
{
double half = pow(x, n/2);
if(n%2 == 0) return half*half;
else return half*half*x;
}
else
{
n = -n;
double half = pow(x, n/2);
if(n%2 == 0) return 1.0/(half*half);
else return 1.0/(half*half*x);
}
}
};