Pow(x, n)
Total Accepted: 13567 Total Submissions: 52439Implement pow(x, n).
public class Solution {
public double pow(double x, int n) {
if(x == 0 || x == 1.0 || n == 1){
return x;
}else if(n == 0){
return 1;
}
int absN = abs(n);
double temp = pow(x, absN/2);
double result = temp*temp;
if(absN%2 != 0 ){
result *= x;
}
if(n<0){
return 1.0/result;
}else{
return result;
}
}
public int abs(int n ){
return n<0? -n:n;
}
}