Skip to content

Latest commit

 

History

History
80 lines (56 loc) · 1.58 KB

0050-powx-n.adoc

File metadata and controls

80 lines (56 loc) · 1.58 KB

50. Pow(x, n)

{leetcode}/problems/powx-n/[LeetCode - 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]

思路分析

{image_attr}
{image_attr}

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

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

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

快速幂的算法能看懂,但代码不知道怎么写。

一刷
link:{sourcedir}/_0050_PowXN.java[role=include]
二刷
link:{sourcedir}/_0050_PowXN_2.java[role=include]