My accepted 55ms c++ solution, easy to understand


  • 0
    W
    class Solution {
    public:
        int rangeBitwiseAnd(int m, int n) {
            int res = 0;
            int i = 1;
            i <<= 30;
            while(i > 0) {
                if((i & m) != (i & n)) {
                    break;
                } else if((i & m) != 0){
                    res |= i;
                }
                i >>= 1;
            }
            
            return res;
        }
    };

Log in to reply
 

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