public class Solution {
public double myPow(double x, int n) {
if(n==0) return 1.0;
int exp=n>0?n:-n;
if(n==Integer.MIN_VALUE &&x>1) //本题的更新点在,当n极小的时候,若x>1,则返回的是0,不是-无穷。
return 0;
double res;
double v=myPow(x,exp/2);
if(exp%2==0)
{
res=v*v;
}
else
{
res=v*v*x;
}
return n>0?res:1/res;
}
}
public double myPow(double x, int n) {
if(n==0) return 1.0;
int exp=n>0?n:-n;
if(n==Integer.MIN_VALUE &&x>1) //本题的更新点在,当n极小的时候,若x>1,则返回的是0,不是-无穷。
return 0;
double res;
double v=myPow(x,exp/2);
if(exp%2==0)
{
res=v*v;
}
else
{
res=v*v*x;
}
return n>0?res:1/res;
}
}