LeetCode *** 50. Pow(x, n)
2016-04-20 10:10
337 查看
题目:
Implement pow(x, n).
分析:
二分。
代码:
Implement pow(x, n).
分析:
二分。
代码:
class Solution { public: double myPow(double x, int n) { if(!n)return 1; int flag=n>0?1:-1; double res=1,tmp=x; while(n){ if(n%2)res*=tmp; tmp=tmp*tmp; n/=2; } if(flag==1)return res; else return 1/res; } };