【leetcode】翻转数位

 

int reverseBits(int num){
    int max = 0,cnt = 0,cntPre = 0;
    while(num)
    {
        if(1 & num) cnt++;
        else
        {
            if(cntPre + cnt + 1 > max) max = cntPre + cnt + 1;
            cntPre = cnt;
            cnt = 0;
        }
        num >>= 1;
    }
    if(cntPre + cnt + 1 > max) max = cntPre + cnt + 1;
    return max;
}

 

posted @ 2020-09-07 19:57  温暖了寂寞  阅读(164)  评论(0编辑  收藏  举报