题解 | #求最大连续bit数#正则 and 位运算
求最大连续bit数
https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2
import java.util.*; import java.util.stream.*; import java.util.regex.*; import java.util.function.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // Matcher matcher = Pattern.compile("(1+)") // .matcher(Integer.toBinaryString(in.nextInt())); // int len = 0; // while (matcher.find()) { // if (matcher.group().length() > len) { // len = matcher.group().length(); // } // } // System.out.println(len); int num = in.nextInt(); int max = 0; int len = 0; while (num > 0) { if ((num & 1) == 1) { len++; } else { if (len > max) { max = len; } len = 0; } num >>= 1; } if (len > max) { max = len; } System.out.println(max); } }