LeetCode刷题记72
50. Pow(x, n)
class Solution {
public double myPow(double x, int n) {
// return Math.pow(x, n); //哈哈哈哈哈哈哈
int f = 0;
if (n < 0) {
if (n == Integer.MIN_VALUE) {
n ++;
f = 1;
}
n = -n;
x = 1.0 / x;
}
double ans = 1;
if (f == 1) ans *= x;
while (n > 0) {
if (n % 2 == 1) {
ans *= x;
}
x *= x;
n /= 2;
}
return ans;
}
}
// x^11 = x^8 * x^2 * x^1 1011
// 15 = 8 + 4 + 2 + 1
4/5
72/150