50. Pow(x, n)

Implement pow(xn).

class Solution {
public:
    double myPow(double x, int n) {
        double res=1;
        long long int p=n;
        if(n<0){
            p=-p;
            x=1/x;
        }
        for(;p;p>>=1){
            
            if(p&1)res*=x;
            x*=x;
        }
        return res;
    }
};

 

posted @ 2017-10-02 16:19  Tsunami_lj  阅读(90)  评论(0编辑  收藏  举报