1 | |
- Pow(x, n)
Medium
Implement pow(x, n), which calculates x raised to the power n (x^n).
Example 1:
1 | |
Example 2:
1 | |
Example 3:
Input: 2.00000, -2 Output: 0.25000 Explanation:$2^{-2}=1/2^2=1/4=0.25$
Note:
- -100.0 < x < 100.0
- n is a 32-bit signed integer, within the range [−2^31, 2^31 − 1]
这是一个二分法问题,主要思路是计算 x 的(1,2,4,8…)次方
然后n次方可以看成是一个二进制转换问题
比如10次方是[0,1,0,1]对应(2,8)
1 | |