Created
September 24, 2018 08:06
-
-
Save namtx/9daba05317bcbb6734e1b1e7962f1000 to your computer and use it in GitHub Desktop.
Bit grap (https://leetcode.com/problems/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 { | |
count := 0 | |
max := 0 | |
isPreviousOne := false | |
for N > 0 { | |
// fmt.Println(N % 2) | |
if N%2 == 1 { | |
if count > max { | |
max = count | |
} | |
count = 1 | |
isPreviousOne = true | |
} else { | |
if isPreviousOne { | |
count++ | |
} | |
} | |
N = N / 2 | |
} | |
// fmt.Println(max) | |
return max | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment