Given a positive integerN, find and return the longest distance between two consecutive 1's in the binary representation ofN.

If there aren't two consecutive 1's, return0.

按照要求做就可以,这种问题即使有什么奇技淫巧也没啥意思

class Solution {
    public int binaryGap(int N) {
        int res = 0;
        int last = -1;

        for(int i = 0; i < 32; i++) {
            if(((N >> i) & 1) == 1) {
                if(last == -1) {
                    last = i;
                } else {
                    res = Math.max(res, i - last);
                    last = i;
                }
            }
        }

        return res;
    }
}

results matching ""

    No results matching ""