Unknown's avatar

868. Binary Gap

868. Binary Gap

class Solution {
    public int binaryGap(int N) {
        String binary = Integer.toBinaryString(N);
        int first = 0;
        while (first < binary.length() && binary.charAt(first) != '1'){
            first ++;}
        if (first == binary.length()) return 0;
        int diff = Integer.MIN_VALUE;
        int current = first;
        for (int i = first+1; i  diff){
                    diff = i - current;
                }
                current = i;
            }
        }
        if (diff == Integer.MIN_VALUE) return 0;
        return diff;
    }
}

Leave a comment