1. 程式人生 > >50. Pow(x, n)

50. Pow(x, n)

code logs bsp cnblogs color return pan ret span

Implement pow(x, n).

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; } };

50. Pow(x, n)