[LeetCode] Pow(x,n) O(logN)
又一个Linkedin面试过的
public class Solution {
public double pow(double x, int n) {
int sign = 1;
if(n < 0) {
n = -n;
sign = -1;
}
if(n == 0) return 1;
double d = pow(x, n/2);
if(n%2 == 0) return sign == 1 ? d*d : 1/(d*d);
else return sign == 1 ? x*d*d:1/(x*d*d);
}
}