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-1n is an integer.x is not zero or n > 0.-104 <= xn <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you need to multiply a number by itself a certain number of times to find the final result. The brute force approach simply starts with the base number and repeatedly multiplies it by itself over and over again until you reach the target count.
Here's how the algorithm would work step-by-step:
class Solution:
def myPow(self, base_number: float, target_count: int) -> float:
if target_count == 0:
return 1.0
# Handle negative exponents by making the count positive and inverting the base later
is_exponent_negative = target_count < 0
absolute_target_count = -target_count if is_exponent_negative else target_count
running_total = base_number
current_count = 1
# Repeatedly multiply the base until we reach the exact target count of multiplications
while current_count < absolute_target_count:
running_total = running_total * base_number
current_count += 1
# If the target was negative, return the reciprocal fraction form of the final accumulated value
if is_exponent_negative:
return 1.0 / running_total
return running_totalInstead of multiplying a number by itself over and over again one step at a time, the optimal approach uses a shortcut based on symmetry and doubling. By repeatedly squaring the running total and cutting the remaining number of multiplications in half, we can reach the final answer almost instantly.
Here's how the algorithm would work step-by-step:
def my_pow(base_number, target_exponent):
# Zero exponent always results in one according to mathematical rules.
if target_exponent == 0:
return 1.0
# Negative exponents mean we invert the base and work with positive exponent.
if target_exponent < 0:
base_number = 1 / base_number
target_exponent = -target_exponent
accumulated_result = 1.0
current_working_base = base_number
while target_exponent > 0:
# Odd exponents require peeling off one factor to make the target even.
if target_exponent % 2 == 1:
accumulated_result *= current_working_base
current_working_base *= current_working_base
target_exponent //= 2
return accumulated_result| Case | How to Handle |
|---|---|
| Exponent n is zero | Return 1.0 immediately since any non-zero number raised to the power of zero equals one. |
| Exponent n is negative | Convert n to its positive equivalent and invert x, taking care of minimum integer overflow by using a long integer. |
| Base x is zero | Return 0.0 when n is positive, though zero to the power of zero is mathematically undefined and usually handled as 1. |
| Base x is negative with odd or even exponents | The binary exponentiation approach naturally preserves the correct sign based on whether n is odd or even. |
| Base x is 1.0 or -1.0 with extremely large exponents | Iterative or recursive logarithmic scaling prevents Time Limit Exceeded by running in logarithmic O(log n) time. |
| Integer n equals Integer.MIN_VALUE (-2147483648) | Casting n to a 64-bit long integer prevents overflow when negating it to compute the positive exponent. |
| Result exceeds maximum floating-point limits | Standard double precision types in the language will automatically handle overflow by returning Infinity or 0.0 underflow. |