best 44ms C++ solution which beats 100% submission


  • 0
    H
    int rangeBitwiseAnd(int m, int n) {
        if (m==n) return m;
        else if (m==0) return 0;
        
        int bitWise = 0, log2_m = log2(m);
        while(log2_m==(int)log2(n)) {
            int pow2 = pow(2, log2_m);
            bitWise |= pow2;
            m -= pow2;
            n -= pow2;
            if (m==0) break;
            log2_m = log2(m);
        }
        
        return bitWise;
    }
    

    0_1472670320107_Screen Shot 2016-08-31 at 1.45.09 PM.png


Log in to reply
 

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