8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 172117f commit 1442611Copy full SHA for 1442611
practice/problems/pow(x,_n)/solution.java
@@ -0,0 +1,33 @@
1
+class Solution {
2
+ public double myPow(double x, int n) {
3
+ if(n>=0) {
4
+ return posPow(x,n);
5
+ } else {
6
+ return negPow(x,n);
7
+ }
8
9
+ private double posPow(double x, int n) {
10
+ if(n==0) {
11
+ return 1;
12
13
+ double y = myPow(x,n/2);
14
+ if(n%2==0) {
15
+ return y*y;
16
17
+ else {
18
+ return x*y*y;
19
20
21
+ private double negPow(double x, int n) {
22
+ if(n==-1) {
23
+ return 1/x;
24
25
26
27
28
29
30
+ return (1/x)*y*y;
31
32
33
+}
0 commit comments