The most terrific and simplest solution accepted as best submission in C, well-explained

  • 3

    Since we are doing bitwise then let's think as it is. AND all numbers from m to n inclusive then all the right different bits will be erased until all bits are identical in the left part; why? the consecutive attribute of the problem -> from m to n inclusive means gradual increments from m to n <font color="#ff0000">each step with another 1 addition causing more bits zero</font>

    Bang! End of Story!

    • space cost O(1).
    • time cost O(1) - > the digits' number is limited under 32 considering the range.

    int rangeBitwiseAnd(int m, int n)
        int count = 0;
        while(m != n)
            m >>= 1;
            n >>= 1;
        return m << count;

  • 0

    nice solution. however, it is just the common solution. not terrific at all

  • 0

    Bang bang bang!

Log in to reply

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