Java | Leetcode Java题解之第338题比特位计数

题目:

Java | Leetcode Java题解之第338题比特位计数_第1张图片

题解:

class Solution {
    public int[] countBits(int n) {
        int[] bits = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            bits[i] = bits[i & (i - 1)] + 1;
        }
        return bits;
    }
}

你可能感兴趣的:(分享,Java,Leetcode,题解)