题解 | #求最大连续bit数#
求最大连续bit数
http://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
DP解
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()) {
int n = sc.nextInt();
String str = Integer.toBinaryString(n);
int[] arr = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
arr[i] = str.charAt(i) - '0';
}
int length = arr.length;
int[] dp = new int[length + 1];
int res = 0;
// dp[0] = 0;
for (int i = 1;i <= length; i++) {
if (arr[i-1] == 1) {
dp[i] = dp[i-1] + 1;
} else {
dp[i] = 0;
}
res = Math.max(res, dp[i]);
}
System.out.println(res);
}
}
}