题解 | #求最大连续bit数#

求最大连续bit数

https://www.nowcoder.com/practice/4b1658fd8ffb4217bc3b7e85a38cfaf2

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        String s = Integer.toBinaryString(a);
        String str = "11111111111111111111111111111111";
        int n = 0;
        while (n <= 32) {
            String s1 = str.substring(n);
            if (s.contains(s1)) {
                break;
            }
            n++;
        }
        System.out.println(32 - n);
    }
}

// public class Main {
//     public static void main(String[] args) {
//         Scanner sc = new Scanner(System.in);
//         int a = sc.nextInt();
//         String s = Integer.toBinaryString(a);
//         int max = s.charAt(0) == '1' ? 1 : 0;
//         int count = max;
//         for (int i = 1; i < s.length(); i++) {
//             if (s.charAt(i - 1) == '0') {
//                 if (s.charAt(i) == '1') {
//                     count = 1;
//                 } else {
//                     count = 0;
//                 }
//             } else {
//                 if (s.charAt(i) == '1') {
//                     count++;
//                 } else {
//                     max = Math.max(max, count);
//                     count = 0;
//                 }
//             }
//         }
//         max = Math.max(max, count);
//         System.out.println(max);
//     }
// }

全部评论

相关推荐

03-30 19:30
石家庄学院 Java
野蛮的柯基在游泳:都能入股了,还得是Java
点赞 评论 收藏
分享
VirtualBool:都去逗他了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务