求一个byte数字对应的二进制数字中1的最大连续数

功能: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1
    
输入: 一个byte型的数字
    
输出: 无
     
返回: 对应的二进制数字中1的最大连续数

 

private static void getIntBinary(int n){
String str = "";
while (n > 0){
int t = n & 1;
str = t + str;
n = n >> 1;
}
System.out.println(str);
}
// 时间复杂度为O(n)
private static void maxBit(String str){
int max = 0;
int count = 0;
for (char c : str.toCharArray()) {
if(c == '0'){
count = 0;
}else {
count++;
if(count > max){
max = count;
}
}
}
System.out.println(max);

}

 

posted @ 2020-07-02 10:39  冬马党  阅读(557)  评论(0)    收藏  举报