4-liner iterative solution O(log n)


  • 0
        double myPow(double x, int n) {        
            double res = 1.0;
            for (long N = (n>0? n : -(long)n); N; x *= x, N >>= 1)
                if (N & 1) res *= x; 
            return n>0? res : 1.0/res;
        }
    

Log in to reply
 

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