题解 | #求最大连续bit数#
求最大连续bit数
https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
package main
import (
"fmt"
)
func main() {
var input int
var inputString string
fmt.Scan(&input)
inputString = fmt.Sprintf("%b", input) //转化为二进制字符串
inputByte := []byte(inputString) //转换为字节数组 比较起来方便些
result := 0
temp := 0
for i := 0; i < len(inputByte); i++ {
if inputByte[i] == '1' {
temp++
if i == len(inputByte)-1 {
if temp > result {
result = temp
temp = 0
}
}
} else {
if temp >= result {
result = temp
}
temp = 0 //temp值每次遇到 0 字节时都初始化为 0
}
}
fmt.Println(result)
}
