Java Binary Gap
public class Solution {
public static int binaryGap(int n) {
String bin = Integer.toBinaryString(n).replaceAll("^0+|0+$", "");
String[] zeroes = bin.split("1", -1);
int gap = 0;
for (String zero : zeroes) {
gap = Math.max(gap, zero.length());
}
return gap;
}
}
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.