Posted on

Description

Submission

class Solution {
public:
    int binaryGap(int n) {
        vector<int> pos;

        for(int i = 0; i < 32; ++i) {
            if((n>>i)&1) pos.push_back(i);
        }

        if(pos.size() < 2) return 0;

        int ret = 0;
        for(int i = 1; i < pos.size(); ++i) {
            ret = max(pos[i] - pos[i-1], ret);
        }

        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *