Skip to content

Latest commit

 

History

History
50 lines (33 loc) · 927 Bytes

0050-powx-n.adoc

File metadata and controls

50 lines (33 loc) · 927 Bytes

50. Pow(x, n)

首先,可以把"一半的计算结果"存储起来,节省一半的递归调用;

其次,没想到还需要处理"无穷"的情况!

另外,思考一下,如果使用迭代来实现?

Implement pow(x, n), which calculates x raised to the power n (xn).

Example 1:

Input: 2.00000, 10
Output: 1024.00000

Example 2:

Input: 2.10000, 3
Output: 9.26100

Example 3:

Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25

Note:

  • -100.0 < x < 100.0

  • n is a 32-bit signed integer, within the range [-231, 2^31 ^- 1]

link:{sourcedir}/_0050_PowXN.java[role=include]