[codility]Binary-gap

// you can also use includes, for example:
// #include <algorithm>
int solution(int N) {
    // write your code in C++98
    //...traverse the binary representation from low digit to high,
    //keep record of maxLen
    int maxLen = 0;
    bool bRightOne = false;
    int curLen = 0;
    while(N)
    {
        int curDigit = N%2;
        N /= 2;
        if(curDigit == 1)
        {
            if(!bRightOne) bRightOne = true;
            else maxLen = max(maxLen, curLen);
            curLen = 0;
        }
        else curLen++;
    }
    //...return result
    return maxLen;
}

你可能感兴趣的:([codility]Binary-gap)