1. 程式人生 > >leetcode 中等題(2)

leetcode 中等題(2)

50. Pow(x, n) (中等)

    double myPow(double x, int n) {
        double ans = 1;
        unsigned long long p;
        if (n < 0) {
            p = -n;
            x = 1 / x;
        } else {
            p = n;
        }
        while (p) {
            if (p & 1)
                ans *= x;
            x 
*= x; p >>= 1; } return ans; }