Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created July 21, 2018 14:45
Show Gist options
  • Save luojiyin1987/7b393dcefa0158d04d57219cc48fb225 to your computer and use it in GitHub Desktop.
Save luojiyin1987/7b393dcefa0158d04d57219cc48fb225 to your computer and use it in GitHub Desktop.
Binary Gap
func binaryGap(N int) int {
maxDis := 0
indexFirst := 0
indexSecond := 0
i := 0
count := 0
for N > 0 {
if(N & 1 ) == 1 {
count +=1
if count ==1 {
indexFirst = i
} else {
indexFirst , indexSecond = i, indexFirst
if maxDis < indexFirst - indexSecond {
maxDis = indexFirst - indexSecond
}
}
}
i+=1
N >>=1
}
return maxDis
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment