classSolution{public:intbinaryGap(intn){intans=0;// d := the distance between any two 1sfor(intd=-32;n;n/=2,++d)if(n%2==1){ans=max(ans,d);d=0;}returnans;}};
1 2 3 4 5 6 7 8 91011121314
classSolution{publicintbinaryGap(intn){intans=0;// d := the distance between any two 1sfor(intd=-32;n>0;n/=2,++d)if(n%2==1){ans=Math.max(ans,d);d=0;}returnans;}}
1 2 3 4 5 6 7 8 910111213
classSolution:defbinaryGap(self,n:int)->int:ans=0d=-32# the distance between any two 1swhilen:ifn%2==1:ans=max(ans,d)d=0n//=2d+=1returnans