leetcode:Pow(x, n)
来源:互联网 发布:什么是金蝶软件 编辑:程序博客网 时间:2024/05/22 00:45
Implement pow(x, n).
没有考虑INT_MIN 和 INT_MAX导致错了两次
贴上挫挫的代码
class Solution {public: double pow(double x, int n) {if (x<0.0000001 && x>-0.000001 && n<0)return x;//it should be false;else if (x<0.0000001 && x>-0.000001)return x;else if(n==0)return 1;else if(x>-1.000001 && x<-0.999999){if(n&1)return x;return -x;}else if(x>0.999999 && x<1.000001)return x;int temp_n = n>0?n:-n;double result = pow_binary(x, temp_n);return n>0?result:(1/result); }private:double pow_binary(double x, int n){if(n==1)return x;else if(n==0)return 1;double result = pow_binary(x,n>>1);result = result*result;if(n&1)result = result*x;return result;}};
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){if(n==INT_MIN)return 1.0 / (pow(x,INT_MAX)*x);elsereturn 1.0 / pow(x,-n);} if(n==0) return 1.0;double ans = 1.0 ;for(;n>0; x *= x, n>>=1){if(n&1>0)ans *= x;}return ans; }};
0 0
- 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)
- Leetcode: Pow(x, n)
- Gathering Statistics for the Cost Based Optimizer (Pre 10g) (Doc ID 114671.1)
- 记录下
- 有关2014工作室学生负责人
- PHP消息发布时间距当前时间差距函数
- hdu-1312
- leetcode:Pow(x, n)
- U3D 精灵
- IOS接口请求传递JSON参数
- eclipse 的任务管理 默认标签,自定义标签
- 简单多面体(三角形网格表示)的惯性张量求解
- Could not load the "" image referenced from a nib in the bundle with identifier XXXXX
- VC创建自定义不规则窗口
- Document对象的使用:hasChildNodes()方法,nodeName、nodeValue、nodeType的简单介绍
- MYSQL 5.6之DBA与开发者指南