50. Pow(x, n)
math
Last updated
Was this helpful?
math
Last updated
Was this helpful?
Implement , which calculates x
raised to the power n
(i.e., x
n
).
Example 1:
Example 2:
Example 3:
Constraints:
-100.0 < x < 100.0
-2
31
<= n <= 2
31
-1
n
is an integer.
Either x
is not zero or n > 0
.
-10
4
<= x
n
<= 10
4
分析
Instead 每次*x, 把步子指数型提高,每次迈x*x的步子,同时n//=2。奇数时候直接*x