LeetCode-338-比特位计数

阅读 67

2021-09-28

比特位计数

解法一:库函数
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)

0 0 举报