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

来源:互联网 发布:视频服务器软件 编辑:程序博客网 时间:2024/06/03 23:40

Implement pow(xn).

要注意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;    }};


0 0
原创粉丝点击