求最大连续bit数

求最大连续bit数

http://www.nowcoder.com/questionTerminal/4b1658fd8ffb4217bc3b7e85a38cfaf2

import java.util.Scanner;

public class Main {

    private int countBiggestOf1(int n) {
        int res = 0;
        int temp = 0;
        while (n != 0) {
            if ((n & 1) == 1) {
                temp++;
            }
            else {
                temp = 0;
            }
            n = n >>> 1;
            res = Math.max(res, temp);
        }
        return res;
    }
    public Main() {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            int res = countBiggestOf1(n);
            System.out.println(res);
        }
    }

    public static void main(String[] args) {
        Main solution = new Main();
    }
}
全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务