public double myPow(double x, int n) {
boolean positive = true;
if (n < 0) {
n = n * -1;
positive = false;
}
double res = 1.0;
for (int i = n; i != 0; i = i / 2) {
if (i % 2 != 0) {
res = res * x;
}
x = x * x;
}
return positive ? res : 1 / res;
}
class Solution {
public:
double myPow(double x, int n) {
if (n < 0) return 1 / power(x, -n);
return power(x, n);
}
double power(double x, int n) {
if (n == 0) return 1;
double half = power(x, n / 2);
if (n % 2 == 0) return half * half;
return x * half * half;
}
};