What is the difference between these two C code?


  • 0
    H

    Runtime: 4 ms

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

    ==============================================================

    Runtime: 0 ms

    double myPow(double x, int n) {
        double ans = 1, p = x;
        /*overflow for INT_MIN*/
        for(long f = (n>=0) ? n : -n; f > 0 ; f >>= 1) {
            if (f & 1) { ans *= p; }
            p *= p;
        }
        return (n>=0) ? ans : 1.0/ans;
    }

Log in to reply
 

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