487.Calculate the Power of a Number
Easy
Given an integer
base and a non-negative integer exponent, return base raised to the power of exponent (base^exponent).
The loopMultiplication LoopStarting result at 1 and multiplying it by base exactly exponent times. version is the direct reading of what a power means — repeated multiplication. The fast exponentiationFast ExponentiationSplitting the exponent in half at each step (squaring the result) instead of subtracting 1 — base^exponent = (base^(exponent/2))² when exponent is even. version — also called binary exponentiation — exploits the fact that base^exponent can be built from a problem roughly HALF its size instead of just one smaller, cutting the work from exponent steps down to about log₂(exponent).
Example 1:
Input: base = 2, exponent = 10
Output: 1024
Example 2:
Input: base = 5, exponent = 0
Output: 1
Example 3:
Input: base = -3, exponent = 3
Output: -27
+ 4 hidden test cases run on Submit.
Constraints:
- ●
-100 ≤ base ≤ 100 - ●
0 ≤ exponent ≤ 20
base =
2
exponent =
10