My accepted c++ solution using a window


  • 0
    S
    class Solution {
    public:
        int rangeBitwiseAnd(int m, int n) {
            int i=0,j=0;
            while(((0x80000000>>i)&m)==0&&((0x80000000>>i)&n)==0&&i<32) i++;
            j=i;
            while(((0x80000000>>i)&m)==((0x80000000>>i)&n)&&i<32) i++;
            int window=(0xffffffff<<(32-i))&(0xffffffff>>j);
            return m&n&window;
        }
    };

Log in to reply
 

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