4ms solution C++


  • 4
    class Solution
    {
    public:
        double myPow(double x, int n)
        {
            bitset<32> bit(abs(n));
            double result = 1;
            for(int i = bit.size()-1; i >= 0; i--)
            {
                result*=result;
                if(bit[i] == 1)
                    result *=x;
            }
            return (n >= 0)?result : 1/result;
        }
    };

Log in to reply
 

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