leetcode No50. Pow(x, n)
2016-07-28 16:27
369 查看
Question:
Implement pow(x, n).Algorithm:
举个例子:假如是3的35次方,35=2^5+2^1+2^0,要注意超出INT_MAX的情况Accepted Code:
class Solution { public: double myPow(double x, int n) { if(n==0)return 1; if(n==1)return x; if(x==1)return x; if(x==-1) { if(n%2==0)return -x; else if(n%2==1)return x; } else if(n==INT_MAX&&x<1.0)return 0; else if(n==INT_MIN&&x>1.0)return 0; int flag=0; if(n<0) flag=1; n=abs(n); double res=1; while(n>0) { int bit=1; double tmp=x; while(bit*bit<n) { tmp=tmp*tmp; bit*=2; } n=n-bit; res*=tmp; if(res==0) return 0; } if(flag==1) { res=(1/res); return res; } else return res; } };
相关文章推荐
- 深入理解Java:注解(Annotation)自定义注解入门
- 学习TOGAF企业架构的必要性——上海信息化培训中心
- <c:forEach>详解
- 分享:孟岩的《理解矩阵》一文
- Neutron-server初始化 — Neutron L2 Agent服务初始化
- 配置网络文件系统
- jenkins插件 build timeout和build timestamp
- 96.Which two operations can be performed on an external table? (Choose two.) A.Create a view on the
- 剑指offer面试题31、34、36、38
- [bzoj3236][Ahoi2013]作业(莫队+树状数组)
- 记一次工作中的不愉快
- 【手写SORM框架】_思想介绍_架构介绍JAVA280
- windows 10 Bitlocker新特性及破解方法简介
- M - Tunnel Warfare
- Java注释@interface的用法
- error: src refspec master does not match any. 错误处理办法
- mysql 增加或删除表里的字段
- Oracle中varchar,varchar2,nvarchar,nvarchar2的区别
- Android 5.1.1 状态栏裁剪以及点击状态栏的事件监听裁剪
- Android GestureDetector手势滑动使用实例讲解