My C++ code (bit op on n)

  • 0

    use the binary representation of n to do the calculation

       class Solution {
            double pow(double x, int n) {
                unsigned int abs_n;
                double res=1;
                abs_n = n>0?n:-n;
                    if(abs_n & 0x01)
                        res *= x;
                    abs_n >>= 1;
                    x *= x;
                if(n<0) res = 1/res;
                return res;

Log in to reply

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