【leetcode】868. 二进制间距

int binaryGap(int n){
    int max=0, pre=-1, cnt=0;
    while(n){       
        if((n&1) == 1 ){
            if(pre>=0 && cnt-pre > max)
                max=cnt-pre;
            pre=cnt;
        }
        cnt++;
        n>>=1;
    }
    return max;
}
原文地址:https://www.cnblogs.com/ganxiang/p/14062878.html