4ms cpp short code


  • -2
    P
    class Solution {
    public:
        double myPow(double x, int n) {
            if(n==0) return 1.0;
            if(n<0) x = 1.0/x;
            double prd= n%2==0?1.0:x;
            n/=2;
            while(n!=0){
                x*=x;
                if(n%2!=0) prd*=x;
                n/=2;
            }
            return prd;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.