Java solution 8ms


  • 0
    W
    public class Solution {
        public int rangeBitwiseAnd(int m, int n) {
            int mask = 1073741824, res = 0;
            for (int i = 30; i >= 0; i--) {
                if ((m & mask) != 0 && (n & mask) != 0) res |= mask;
                else if ((m & mask) != (n & mask)) break;
                mask /= 2;
            }
            return res;
        }
    }

Log in to reply
 

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