best 38ms solution which beats 100% submission


  • 0
    B

    class Solution {
    public:
    int rangeBitwiseAnd(int m, int n) {
    bitset<32> a(m);
    bitset<32> b(n);
    bitset<32> c(0);
    int an=0;
    while(m>0){
    ++an;
    m=m>>1;
    }
    int bn=0;
    while(n>0){
    ++bn;
    n=n>>1;
    }
    if(an!=bn){
    return 0;
    }
    for(int i=an;i>=0;--i){
    if(a[i]!=b[i]){
    break;
    }
    c[i]=a[i];
    }
    return c.to_ulong();
    }
    };


Log in to reply
 

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