Simple and easy to understand java solution


  • 36
    Z
    public class Solution {
        public int rangeBitwiseAnd(int m, int n) {
            int diffBits = 0;
            while (m != n) {
                m >>= 1;
                n >>= 1;
                diffBits++;
            }
            return n<<diffBits;
        }
    }
    

    It's a problem that can be reduced to find the same prefix of the numbers in this range.


  • 0
    D

    great thought to find the max prefix of binary numbers.


Log in to reply
 

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