Easy UnderStand Solution in Java


  • 0
    _

    /*
    * Solution: 1. First find how many numbers between m and n;
    * 2. more than 1 number between them means the lowest bit changed between 0 and 1;
    * more than 2 numbers means the second bit changed
    * more than 4 numbers means the third bit changed
    * So we need find the highest 1 in the n-m and set the lower bits 0 higher bit 1, (if n-m == 1011 then we set x = 11...110000)
    * 3. lower bits depend on x while higher bits depend on m ^ n so the result = m & n & x
    */

    “”“

      public int rangeBitwiseAnd(int m, int n) {
    	if (m == n)
    		return m;
    	int x = format(n - m);
    
    	return m & n & x;
    
    }
    
    private int format(int x) {
    	int i = 31;
    	while (((1 << i) & x) == 0)
    		i--;
    	return ~((1 << (i + 1)) - 1);
    }
    

    ”“”


Log in to reply
 

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