C++ 52m code, beat 100%


  • 0
    S
    class Solution{
    public:
          int rangeBitwiseAnd(int m,int n){
               int m0=m,n0=n,ind=0,i=0,mask=0xFFFFFFFF;
               while(n0>0){
                     if((m0^n0)&1)ind=i+1;
                     i++;
                     m0>>=1;n0>>=1;
               }
               for(i=0;i<ind;i++)mask<<=1;
               return n&mask;
          }
    };

Log in to reply
 

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