[LeetCode] 077: Pow(x, n)
来源:互联网 发布:点餐系统数据库设计 编辑:程序博客网 时间:2024/05/17 23:22
[Problem]
Implement pow(x, n).
[Solution]
Implement pow(x, n).
[Solution]
class Solution {说明:版权所有,转载请注明出处。Coder007的博客
public:
double pow(double x, int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
// special cases
if(n == 0)return 1;
if(fabs(x - 0) < 0.0000000001)return 0;
// x == 1
if(fabs(x - 1) < 0.0000000001)return 1;
// x == -1
if(fabs(x + 1) < 0.0000000001){
if(n % 2 == 0)
return 1;
else
return -1;
}
// x < 0
if(x < 0){
if(n % 2 == 0){
return pow(-x, n);
}
else{
return -pow(-x, n);
}
}
// n < 0
if(n < 0){
return 1/pow(x, -n);
}
if(n == 1){
return x;
}
// n is even
if(n % 2 == 0){
double half = pow(x, n/2);
return half * half;
}
else{
double half = pow(x, n/2);
return half * half * x;
}
}
};
阅读全文
0 0
- [LeetCode] 077: Pow(x, n)
- LeetCode: Pow(x, n)
- LeetCode Pow(x,n)
- [Leetcode] Pow(x, n)
- LeetCode: Pow(x, n)
- Leetcode: Pow(x,n)
- [LeetCode] Pow(x, n)
- [Leetcode] Pow(x,n)
- 【leetcode】Pow(x, n)
- [LeetCode]Pow(x, n)
- [leetcode]Pow(x, n)
- LeetCode-Pow(x, n)
- LeetCode - Pow(x, n)
- LeetCode | Pow(x, n)
- Pow(x, n) -leetcode
- 【leetcode】Pow(x, n)
- 【Leetcode】Pow(x, n)
- LeetCode Pow(x, n)
- linux iic驱动编程之一 基本的编程思想
- [LeetCode] 075: Populating Next Right Pointers in Each Node
- u3d响应点击事件
- [LeetCode] 076: Populating Next Right Pointers in Each Node II
- 《利用 Python 进行数据分析》读书笔记
- [LeetCode] 077: Pow(x, n)
- [转载] 从Trie树到后缀树
- 薛定谔的猫
- Windows python3下安装scrapy爬虫框架的完美方案
- [面试题] 随机数相关面试题(1)
- [面试题] 随机数相关面试题(2)
- [LeetCode] 078: Recover Binary Search Tree
- 下拉刷新,上拉加载
- [LeetCode] 079: Regular Expression Matching