题解 | #求最大连续bit数#

求最大连续bit数

http://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2

import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int number = scanner.nextInt();
/**
*byte number=scanner.nextByte();
byte[] bytes = new byte[8];
for (int i = 0; i < 8; i++) {
bytes[bytes.length - i - 1] = (byte) (number & 1);
number = (byte) (number >> 1);
}
*/

        String binaryString = Integer.toBinaryString(number);
        String[] strings = binaryString.split("0+"); //再将字符串按多个0分割
        int count = 0;
        for (String string : strings) {
            count = Math.max(count, string.length());
        }
        System.out.println(count);
    }
}

}

全部评论

相关推荐

05-22 12:44
已编辑
门头沟学院 golang
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务