[LeetCode]Pow(x, n)
2013-05-31 10:39
316 查看
class Solution { //divide-and-conquer //classic public: double pow(double x, int n) { if (n == 0) return 1.0; // Compute x^{n/2} and store the result into a temporary // variable to avoid unnecessary computing double half = pow(x, n / 2); if (n % 2 == 0) return half * half; else if (n > 0) return half * half * x; else return half * half / x; } };
second time
class Solution { public: double pow(double x, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n == 0) return 1; else if(n > 0) { double half = pow(x, n/2); if(n%2 == 0) return half*half; else return half*half*x; } else { n = -n; double half = pow(x, n/2); if(n%2 == 0) return 1.0/(half*half); else return 1.0/(half*half*x); } } };
相关文章推荐
- Leetcode::pow
- leetcode[50]Pow(x, n)
- [Leetcode] Pow(x, n) (Java)
- https://leetcode.com/problems/powx-n/
- LeetCode: Pow(x, n)
- LeetCode之Pow(x, n)
- LeetCode--Pow(x, n)
- leetcode-pow(x,n)
- LeetCode 050 Pow(x, n)
- leetcode 刷题之路 36 Pow(x, n)
- leetcode之Pow(x, n)
- LeetCode Pow(x, n)
- 【LeetCode】Pow(x, n)
- [LeetCode]75. Pow(x,n)幂运算
- [Leetcode]Pow(x, n)
- [leetcode]Pow(x, n) @ Python
- LeetCode 第 50 题 (Pow(x, n))
- leetcode_372. Super Pow ? 待解决
- (java)leetcode-50:Pow(x, n)
- LeetCode Pow(x,n)