leetcode-50-pow(x, n)

https://leetcode.com/problems/powx-n/
Implement pow(x, n), which calculates x raised to the power n (xn).

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
27
28
29
public double (double x, int n) {
if(n > 0 && n <= 10) {
double t = x;
while(n > 1) {
t *= x;
n--;
}
return t;
}
if(n < 0 && n >= -10) {
x = 1/x;
double t = x;
while(n < -1) {
t *= x;
n++;
}
return t;
}
if(n == 0) {
return 1;
}
double haft = myPow(x, n >> 1);
double haft2 = haft * haft;
if((n & 1)== 1) {
return haft2 * x;
} else {
return haft2;
}
}