Skip to content

Commit b7a9830

Browse files
authored
Create 50. Pow(x, n).java
1 parent 9559be4 commit b7a9830

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

50. Pow(x, n).java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public double myPow(double x, int n) {
3+
if (n == 0 || x == 1) {
4+
return 1;
5+
}
6+
7+
if (n < 0) {
8+
x = 1/x;
9+
// very silly workaround
10+
n =(n == Integer.MIN_VALUE) ? Integer.MAX_VALUE - 1 : -n;
11+
}
12+
13+
return (n % 2 == 0) ? myPow(x*x, n/2) : x * myPow(x*x, n/2);
14+
}
15+
}

0 commit comments

Comments
 (0)