顿搜
飞过闲红千叶,夕岸在哪
类目归类
Implement pow(x, n).
2^-2147483648 = 0.0
public double myPow(double x, int n) {
if (n == 0) {
return 1;
}
double tmp = x, result = 1.0;
long y = n;
for (y = y < 0 ? -y : y; y != 0; y = y >> 1) {
if ((y & 1) == 1) {
result *= tmp;
}
tmp *= tmp;
}
return n < 0 ? 1 / result : result;
}public class LeetCode0050 {
public double myPow(double x, int n) {
if (n == 0) {
return 1;
}
double tmp = x, result = 1.0;
long y = n;
for (y = y < 0 ? -y : y; y != 0; y = y >> 1) {
if ((y & 1) == 1) {
result *= tmp;
}
tmp *= tmp;
}
return n < 0 ? 1 / result : result;
}
public static void main(String[] args) {
LeetCode0050 leetcode = new LeetCode0050();
System.out.println(leetcode.myPow(2, -2147483648));
}
}