0
点赞
收藏
分享

微信扫一扫

LeetCode-338-比特位计数

天际孤狼 2021-09-28 阅读 67
LeetCode

比特位计数

解法一:库函数
public class LeetCode_338 {
    /**
     * 使用库函数 Integer.bitCount 直接获取整数对应的二进制的1的个数
     *
     * @param n
     * @return
     */
    public static int[] countBits(int n) {
        int[] result = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            result[i] = Integer.bitCount(i);
        }
        return result;
    }

    public static void main(String[] args) {
        for (int i : countBits(100)) {
            System.out.println(i);
        }
    }
}
举报

相关推荐

0 条评论