Pow(x, n) Leetcode Solution in java | Hindi Coding Community

0

 


Implement pow(x, n), which calculates x raised to the power n (i.e., xn).

Example 1:


Input: x = 2.00000, n = 10

Output: 1024.00000



double myPow(double x, int n) {
if(n<0) return 1/x * myPow(1/x, -(n+1));
if(n==0) return 1;
if(n==2) return x*x;
if(n%2==0) return myPow( myPow(x, n/2), 2);
else return x*myPow( myPow(x, n/2), 2);
}

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !