LeetCode 50: Pow(x, n)

Implement pow(x, n).

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
double myPow(double x, int n) {
double res = 1;
double flag = x;
int m = n;
if (n < 0) {
x = 1 / x;
if (n == INT_MIN) {
n = INT_MAX;
} else {
n = -n;
}
}
while (n>0) {
if (n & 1 == 1) {
res = res*x;
}
x *= x;
n = n >> 1;
}
if (m == INT_MIN) {
if (flag < 0) {
return -res;
}
}
return res;
}
分享到 评论