题解 | #最长回文子串#
求最大连续bit数
http://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int sum = 0;
int count = 0;
for(int i = 0; i < 32; ++i){
if(((n>>>i)&1) == 1){
sum++;
count = Math.max(count,sum);
}else{
sum = 0;
}
}
System.out.println(count);
}
}
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int sum = 0;
int count = 0;
for(int i = 0; i < 32; ++i){
if(((n>>>i)&1) == 1){
sum++;
count = Math.max(count,sum);
}else{
sum = 0;
}
}
System.out.println(count);
}
}
}
利用两个变量sum,count来分别存储当前位是否为1,连续为1的最大长度,不断地将count进行迭代,最终获取到最长的连续的二进制1