[Leetcode] 50. Pow(x, n) 解题报告
来源:互联网 发布:js select 赋值选中 编辑:程序博客网 时间:2024/06/05 00:22
题目:
Implement pow(x, n).
思路:
求幂、求平方根等题目的最优解法就是二分了。本题需要注意的地方在于:1)指数为负数的情况;2)指数为0的情况;3)指数为奇数和偶数的不同情况。时间复杂度为O(logn)。
从理论上来讲,Pow(0,0)是没有定义的,下面的代码实际上返回了1。在面试中最好和面试官沟通这种特殊情况如何处理。
代码:
class Solution {public: double myPow(double x, int n) { return Pow(n < 0 ? 1 / x : x, n); }private: double Pow(double x, int n) { if(n == 0) return 1; // special case else if(n == 1 || n == -1) return x; // base case double half = Pow(x, n/2); return half * half * (n % 2 == 0 ? 1 : x); }};
0 0
- [leetcode] 50. Pow(x, n) 解题报告
- [Leetcode] 50. Pow(x, n) 解题报告
- Leetcode Pow(x, n) 解题报告
- LeetCode-Pow(x, n)-解题报告
- [LeetCode] Pow(x, n) 解题报告
- Leetcode #50. Pow(x, n) 幂实现 解题报告
- [leetcode]50. Pow(x, n)@Java解题报告
- [LeetCode]pow(double x,int n)解题报告
- [LeetCode]50.Pow(x, n)
- LeetCode --- 50. Pow(x, n)
- [Leetcode] 50. Pow(x, n)
- [leetcode] 50.Pow(x, n)
- LeetCode 50. Pow(x, n)
- 50. Pow(x, n) LeetCode
- [leetcode] 50. Pow(x, n)
- Leetcode 50. Pow(x, n)
- [LeetCode]50. Pow(x, n)
- LeetCode *** 50. Pow(x, n)
- 242. Valid Anagram
- 最短路径应用题
- 生成英文回文串
- 序列求和
- 树莓派日记——树莓派3的新手入门
- [Leetcode] 50. Pow(x, n) 解题报告
- 最大子数组问题
- 80老翁谈人生(337):网络空间是否具有地理边界?
- MacOS下安装rvm的几点注意
- Mesos实践:基于MZM的PAAS的构建(3):Marathon简介
- Leetcode 290. Word Pattern
- 二、算法_二叉树
- Http状态码与常见问题出现的原因
- Oracle client uninstall and reinstall