Improved(infinity, out of int range) bit operation solution C++


  • 0
    X

    class Solution {
    public:
    double myPow(double x, int n) {
    long int ln = n;
    if (ln == 0)
    {
    return 1;
    }
    if (x == 0 && ln <0)
    {
    return std::numeric_limits<double>::infinity();
    }
    else if (x !=0 && ln <0)
    {
    ln= -ln;
    x = 1/x;
    }
    double ans =1;
    while (ln>0)
    {
    if (ln&1) ans*= x;
    x = x*x;
    ln >>= 1;
    }

        return ans;
        
    }
    

    };


Log in to reply
 

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