Implement pow(x, n).
思路: We need to using binary to get a lg(n) method. So, we need to get pow(x, n /2); At this situation, we have to consider the even or odd number. If the n is a odd number, there are two situation positive number or negative number.
易错点: n== 0 时候返回1, n为奇数时候要考虑 正负数, 因为一种情况要 多乘个 x , 负数要多 除个n
public class Solution {
public double pow(double x, int n) {
if(n == 0)
return 1;
double ret = 0.0;
double a = pow(x, n / 2);
if(n % 2 == 0){
return a * a ;
}else if(n > 0){
return a * a * x;
}else{
return a * a / x;
}
}
}