[LeetCode] 50. Pow(x, n) 求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, 231 − 1]
这道题让我们求x的n次方,如果只是简单的用个 for 循环让x乘以自己n次的话,未免也把 LeetCode 上的题想的太简单了,一句话形容图样图森破啊。OJ 因超时无法通过,所以需要优化,使其在更有效的算出结果来们可以用递归来折半计算,每次把n缩小一半,这样n最终会缩小到0,任何数的0次方都为1,这时候再往回乘,如果此时n是偶数,直接把上次递归得到的值算个平方返回即可,如果是奇数,则还需要乘上个x的值。还有一点需要引起注意的是n有可能为负数,对于n是负数的情况,我可以先用其绝对值计算出一个结果再取其倒数即可,之前是可以的,但是现在 test case 中加了个负2的31次方后,这就不行了,因为其绝对值超过了整型最大值,会有溢出错误,不过可以用另一种写法只用一个函数,在每次递归中处理n的正负,然后做相应的变换即可,代码如下:
解法一:
class Solution { public: double myPow(double x, int n) { if (n == 0) return 1; double half = myPow(x, n / 2); if (n % 2 == 0) return half * half; if (n > 0) return half * half * x; return half * half / x; } };
这道题还有迭代的解法,让i初始化为n,然后看i是否是2的倍数,不是的话就让 res 乘以x。然后x乘以自己,i每次循环缩小一半,直到为0停止循环。最后看n的正负,如果为负,返回其倒数,参见代码如下:
解法二:
class Solution { public: double myPow(double x, int n) { double res = 1.0; for (int i = n; i != 0; i /= 2) { if (i % 2 != 0) res *= x; x *= x; } return n < 0 ? 1 / res : res; } };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/50
类似题目:
参考资料:
https://leetcode.com/problems/powx-n/
https://leetcode.com/problems/powx-n/discuss/19733/simple-iterative-lg-n-solution
https://leetcode.com/problems/powx-n/discuss/19546/Short-and-easy-to-understand-solution
https://leetcode.com/problems/powx-n/discuss/19544/5-different-choices-when-talk-with-interviewers
相关文章
- Leetcode: H-Index
- [LeetCode]剑指 Offer 50. 第一个只出现一次的字符
- 【数据结构/哈希表】leetcode刷题路线(持续更新)
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
- LeetCode 8. 字符串转换整数 (atoi)
- [LeetCode] 1219. Path with Maximum Gold 黄金矿工
- [LeetCode] 1209. Remove All Adjacent Duplicates in String II 移除字符串中所有相邻的重复字符之二
- [LeetCode] 1039. Minimum Score Triangulation of Polygon 多边形三角形化的最小得分
- [LeetCode] 552. Student Attendance Record II 学生出勤记录之二
- [LeetCode] Binary Watch 二进制表
- [LeetCode] 347. Top K Frequent Elements 前K个高频元素
- [LeetCode] Tenth Line 第十行
- [LeetCode] 254. Factor Combinations 因子组合
- [LeetCode] 248. Strobogrammatic Number III 对称数之三
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
- [LeetCode] 179. Largest Number 最大组合数