MediumRecursionMathDivide and Conquer

Pow(x, n)

LeetCode
2 approaches, code in all languages

Implement exponentiation. Given a real base x and an integer exponent n, return x raised to the power n, written x^n.

The exponent can be zero or negative. A negative exponent means the reciprocal of the corresponding positive power, so x^(-k) is 1 divided by x^k.

Example 1

Input: x = 2.00000, n = 10

Output: 1024.00000

Example 2

Input: x = 2.00000, n = -2

Output: 0.25000

2^(-2) = 1 / 2^2 = 1 / 4 = 0.25.

Constraints

  • −100.0 < x < 100.0
  • −2^31 ≤ n ≤ 2^31 − 1 and n is an integer
  • Either x is non-zero or n is greater than 0
  • The final result stays within the range [−10^4, 10^4]
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now