Algorithm


Problem Name: 50. Pow(x, n)

Problem Link: https://leetcode.com/problems/powx-n/

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

Example 2:

Input: x = 2.10000, n = 3
Output: 9.26100

Example 3:

Input: x = 2.00000, n = -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25

Constraints:

  • -100.0 < x < 100.0
  • -231 <= n <= 231-1
  • n is an integer.
  • -104 <= xn <= 104

Code Examples

#1 Code Example with C Programming

Code - C Programming


double powx(double x, int n) {
    double k;
    if (n == 0) return 1;
    k = powx(x * x, n / 2);
    if (n % 2) k = k * x;
    return k;
}
double myPow(double x, int n) {
    if (n  <  0) { x = 1 / x; n = 0 - n; }
    return powx(x, n);
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
x = 2.00000, n = 10

Output

x
+
cmd
1024.00000

#2 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    double myPow(double x, int n) {
        if(x == 1) return 1;
        if(x == -1) return n % 2 ? -1 : 1;
        if(n == INT_MIN) return 0;
        if(n == 0) return 1;
        if(n < 0) return 1 / myPow(x, -n);
        if(n % 2) return x * myPow(x, n - 1);
        else{
            double d = myPow(x, n/2>;
            return d * d;
        }
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
x = 2.10000, n = 3

Output

x
+
cmd
9.26100

#3 Code Example with Java Programming

Code - Java Programming


class Solution {
  public double myPow(double x, int n) {
    if (n == 0) {
      return 1;
    }
    if (n  <  0) {
      return 1 / x * myPow(1 / x, -1 * (n + 1));
    }
    return n % 2 == 0 ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2);
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
x = 2.00000, n = -2

Output

x
+
cmd
0.25000

#4 Code Example with Javascript Programming

Code - Javascript Programming


const myPow = function(x, n) {
  if (n === 0) return 1;
  if (n === 1) return x;
  if (x === 0) return 0;

  if (n > 0) {
    return (n % 2 === 1 ? x : 1) * myPow(x * x, Math.floor(n / 2));
  } else {
    return myPow(1 / x, -n);
  }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
x = 2.00000, n = 10

Output

x
+
cmd
1024.00000

#5 Code Example with Python Programming

Code - Python Programming


class Solution:
    def myPow(self, x: float, n: int) -> float:
        if n < 0:
            n *= -1
            x = 1 / x
        elif not n:
            return 1
        half = self.myPow(x, n // 2)
        return x * half * half if n % 2 else half * half
Copy The Code & Try With Live Editor

Input

x
+
cmd
x = 2.10000, n = 3

Output

x
+
cmd
9.26100

#6 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _050_Pow
    {
        public double MyPow(double x, int n)
        {
            if (x == 0 || x == 1) return x;
            if (n == 0) return 1;

            var temp = MyPow(x, n / 2);

            if (n % 2 == 0) return temp * temp;
            return n > 0 ? x * temp * temp : (temp * temp) / x;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
x = 2.00000, n = -2

Output

x
+
cmd
0.25000
Advertisements

Demonstration


Previous
#49 Leetcode Group Anagrams Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#51 Leetcode N-Queens Solution in C, C++, Java, JavaScript, Python, C# Leetcode