C++ 28ms


  • 0
    F
    int rangeBitwiseAnd(int m, int n) {
        int ans = m;
        int diff = n - m;
        for(int i=0; (m>>i) > 0; ++i) {
            int a = 1<<i;
            if( ((m % a) + diff) >= a ) {
                //set bit to 0
                ans = ans & (~a);
            }
        }
        return ans;
    }

Log in to reply
 

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