题解 | #求最大连续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;
    }
}
全部评论

相关推荐

Noob1024:一笔传三代,人走笔还在
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务