版权声明:原创勿转 https://cloud.tencent.com/developer/article/1420471
N不断右移的同时,判断是否等于1
func binaryGap(N int) int {
var d, max int
if N&(N-1) == 0 {
return 0
}
for ; N > 0; N >>= 1 {
if N&1 == 1 {
if max == 0 {
max = 1
}
if d > max {
max = d
}
d = 0
}
if max > 0 {
d++
}
}
return max
}