Created
July 21, 2018 14:45
-
-
Save luojiyin1987/7b393dcefa0158d04d57219cc48fb225 to your computer and use it in GitHub Desktop.
Binary Gap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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