题目链接:https://leetcode.com/problems/powx-n/#/description
Implement pow(x, n).
class Solution {
public:
double myPow(double x, int n) {
if (n == 0)
return 1.0;
else {
if (n < 0)
{
//判断是否溢出
if (n == INT_MIN)
return 1.0 / (myPow(x, INT_MAX)*x);
else
return 1.0 / myPow(x, -n);
}else{
if (n % 2 == 0)
{
double temp = myPow(x, n >> 1);
return temp * temp;
}
else{
double temp = myPow(x, (n - 1) >> 1);
return temp * temp * x;
}
}//else
}//else
}
};