您的位置:首页 > 产品设计 > UI/UE

[leetcode] Implement pow(x, n). Subscribe to see which companies asked this question

2016-09-03 07:32 381 查看
Implement pow(x, n).

要注意n<0的情况。

class Solution {
public:
double myPow(double x, int n) {
if(n<0) return 1/power(x,-n);
else return power(x,n);
}

double power(double x, int n){
double res;
if(n==0) return 1;
double half = power(x,n/2);
if(n%2==0) return half*half;
else return x*half*half;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode medium