您的位置:首页 > 其它

50 Pow(x, n)(求x的n次方Medium)

2015-06-13 01:51 260 查看
题目意思:x为double,n为int,求x的n次方

思路分析:直接求,注意临界条件

class Solution {
public:
double myPow(double x, int n) {
if(x==1.0)return x;
else if(x==-1.0){
if(n%2==0)return 1.0;
else return -1.0;
}
double ans=1.0;
int flag=abs(n);
while(flag--&&abs(ans)>0.0000001)ans*=x;
if(n>0)return ans;
else return 1.0/ans;
}
};


时间复杂度:O(n)

运行时间:20ms

此题并没有用什么算法,有时间再研究
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: