leetcode 50: Pow(x, n)
2015-07-27 18:09
435 查看
Loop through each bit of n instead of n times. Be careful with the case that when n is INT_MIN.
class Solution {
public:
double myPow(double x, int n) {
if(n==0)
return 1;
if(n<0)
{
if(n==INT_MIN)
return 1.0/(myPow(x,INT_MAX)*x);
else
return 1.0/myPow(x,-n);
}
double res=1;
while(n>0)
{
if(n&1)
res*=x;
x*=x;
n>>=1;
}
return res;
}
};
class Solution {
public:
double myPow(double x, int n) {
if(n==0)
return 1;
if(n<0)
{
if(n==INT_MIN)
return 1.0/(myPow(x,INT_MAX)*x);
else
return 1.0/myPow(x,-n);
}
double res=1;
while(n>0)
{
if(n&1)
res*=x;
x*=x;
n>>=1;
}
return res;
}
};
相关文章推荐
- 理解图像中的高频分量和低频分量
- Eclipse使用中的奇奇怪怪问题
- Implement Trie (Prefix Tree) ——LeetCode
- navicat的注册和安装问题
- 浅谈数组与指针表示字符串的区别
- HTML—超文本标记语言
- 【二分图匹配】zoj1002Fire Net
- System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration sect
- Python实现删除当前目录下除当前脚本以外的文件和文件夹实例
- git操作分支
- LeetCode_105 Construct Binary Tree from Preorder and Inorder Traversal
- 注入式开发
- System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration sect
- code Gym 100500D T-shirts(暴力)
- .NET学习笔记 -- 那堆名词到底是啥(CLR、CLI、CTS、CLS、IL、JIT)
- 注入式开发
- oracle 任务使用
- JavaScript中的Repaint和Reflow用法详解
- breeze densematrix 基本操作
- apache worker性能调优