7-line Very Easy Understand Java Solution


  • 0
    H

    If m != n, then m - n >= 1. So the last bit must be 0. Then count this digit, and simply move m and n for one bit, and judge again...

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

Log in to reply
 

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