Problem · Math
Power with Integer Exponents
Learn this problemProblem statement
Implement myPow(x, n), which returns the real number x raised to the signed 32-bit integer exponent n.
Your implementation must handle zero and negative exponents and must not overflow when negating the minimum 32-bit integer.
Function
myPow(x: double, n: int) → doubleExamples
Example 1
x = 2.0n = 10return = 1024.0Multiplying ten factors of 2 gives 1024.
Example 2
x = 2.0n = -2return = 0.25A negative exponent uses the reciprocal: 2^-2 = 1 / 4.
Constraints
nis any signed 32-bit integer.- If
xis0, thennis positive. - Answers are compared with relative or absolute tolerance
10^-9.