Pow(x, n) LeetCode solution in Java

1
class Solution {
2
public double myPow(double x, int n) {
3
if (n == 0) return 1;
4
if (n == 1) return x;
5
else if (n == -1) return 1 / x;
6
double res = myPow(x, n / 2);
7
if (n % 2 == 0) return res * res;
8
else if (n % 2 == -1) return res * res * (1 / x);
9
else return res * res * x;
10
}
11
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0