Skip to content

Commit 586aecb

Browse files
50. Pow(x, n) (java)
1 parent ed4b249 commit 586aecb

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

solution/0050.Pow(x, n)/Solution.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public double myPow(double x, int n) {
3+
if(n < 0) return sum(1.0 / x,0 - n);
4+
return sum(x,n);
5+
}
6+
7+
public double sum(double x, int n) {
8+
if(n == 0) return 1;
9+
if(n == 1) return x;
10+
if( n % 2 == 0) return sum(x * x, n / 2);
11+
else return x * sum(x * x, n / 2);
12+
}
13+
}

0 commit comments

Comments
 (0)