C++ solution, many lines


  • 0
    U
    class Solution {
    public:
        int rangeBitwiseAnd(int m, int n) {
            if(m==n || m==0) return m;
            int ib=0; 
            int result = 0;
            while(ib<32){
                ib++;
                int nb = pow(2,ib);
                if (nb > n) break;
                if(n - m + 1 > nb) continue;
                if((m / nb)%2==0 || (n / nb)%2==0) continue;
                result |= 1 << ib;
            }
            return result;
        }
    };
    

    My second post ;)


Log in to reply
 

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