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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s