Topic
answer
Pit 1. There may be negative numbers in this question. Use fast power to convert it to a positive number, and finally take the reciprocal.
Pit 2. When n is equal to negative infinity, taking the opposite number will exceed the range of int. Long long
Code
class Solution {
public:
double Power(double x, int n) {
typedef long long LL;
bool is_minus = n < 0;
double res = 1;
LL k = abs(LL(n));
while (k) {
if (k & 1) res = res * x;
x = x * x;
k >>= 1;
}
if (is_minus) res = 1 / res;
return res;
}
};