记录两个1之间的距离,要用flag来判断是否第一次出现1
class Solution {
public int binaryGap(int N) {
if(N==0) return 0;
int count = 0,max = Integer.MIN_VALUE;
boolean flag = true;
while(N>0){
int tmp = N%2;
if(tmp==1&&flag){
count=1;
flag=false;
}else if(tmp==1){
max = Math.max(max,count);
count=1;
}else{
count++;
}
N /= 2;
}
return max>0?max:0;
}
}