Question:
Implement pow(x, n).
public class Solution {
public double pow(double x, int n) {
if (n >= 0)
return positivePow(x, n);
return 1 / positivePow(x, -n);
}
private double positivePow(double x, int n) {
if (n == 0)
return 1;
if (n == 1)
return x;
double temp;
if (n % 2 == 0) {
temp = positivePow(x, n / 2);
return temp * temp;
}
temp = positivePow(x, n / 2);
return temp * temp * x;
}
}