An easy to understand method defeats 97.2%


  • 0
    J

    Just to count the bits used between the range and mask these bits.

    public int rangeBitwiseAnd(int m, int n) {
            int cnt= 0;
            int diff= n - m;
            while(diff!=0){
                diff= diff>>1;
                cnt++;
            }
            int result= 0;
            result= m & n;
            int mask= -1;
            for(int i=0; i<cnt; i++){
                mask= mask<<1;
            }
            result= result & mask;
            return result;
        }

Log in to reply
 

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