题解 | #求最大连续bit数#
求最大连续bit数
http://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
import javax.swing.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
String s = Integer.toBinaryString(n);
int res = getRes(s);
System.out.println(res);
}
}
private static int getRes(String s) {
int count = 1;
StringBuilder sb = new StringBuilder(s);
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == '1') {
int num = 1;
for (int j = i + 1; j < sb.length(); j++) {
if(sb.charAt(j) == '1'){
num++;
count = Math.max(num, count);
}else{
break;
}
}
}
}
return count;
}
}