题目:
Implement pow(x, n).
Subscribe to see which companies asked this question
分析:n属于INT_MIN时的n=0-n会溢出n的最大值,需特别考虑
代码:
class Solution {
public:
double myPow(double x, int n) {
if(n==0)
{
return 1;
}
if(n<0)
{
if(n==INT_MIN)
{
return (1/x)*myPow(x,n+1);
}
x=1/x;
n=0-n;
}
if(n%2==0)
{
return myPow(x*x,n/2);
}
else
{
return myPow(x*x,n/2)*x;
}
}
};