Iterative c++ solution runs in 0ms


  • 3
    W
    double myPow(double x, int m) {
        double ans = 1.0;
        double p = x;
        long n = (m >= 0) ? m : -m;
        while(n > 0) {
            if (n & 1) {
                ans *= p;
            }
            n >>= 1;
            p *= p;
        }
        return (m >=0) ? ans : 1.0/ans;
    }

  • 0
    D

    I always find bit manipulation amazing.


Log in to reply
 

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