n is a 32-bit signed integer, within the range [−231, 231 − 1]
class Solution {
public double myPow(double x, int n) {
double ans = 1;
long absN = Math.abs((long) n);
while (absN > 0) {
if (absN % 2 != 0)
ans *= x;
absN /= 2;
x *= x;
}
return n < 0 ? 1 / ans : ans;
}
}