class Solution {
public double myPow(double x, int n) {
long nn = n;
return myPow(x,nn);
}
private double myPow(double x, long n) {
if(n==0) return 1;
if(n<0){
n = n;
x = 1/x;
}
return n%2==0 ? myPow(x*x, n/2) : x*myPow(x*x, n/2);
}
}
S
shubhamvadhera
@shubhamvadhera
1
Reputation
23
Posts
115
Profile views
0
Followers
0
Following
Posts made by shubhamvadhera

Solution that actually works  all test cases pass

RE: Short and easy to understand solution
@peiheng This doesn't work now. Here is the modified code:
class Solution { public double myPow(double x, int n) { long nn = n; return myPow(x,nn); } private double myPow(double x, long n) { if(n==0) return 1; if(n<0){ n = n; x = 1/x; } return n%2==0 ? myPow(x*x, n/2) : x*myPow(x*x, n/2); } }