【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;
}

 

posted @ 2020-11-30 18:41  温暖了寂寞  阅读(71)  评论(0编辑  收藏  举报