剑指offer 前n个数二进制中1的个数

力扣题目链接
位运算

class Solution {
    public int[] countBits(int n) {
        int[] nums = new int[n+1];
        for(int i=0;i<=n;++i){
            for(int j=0;j<32;++j){
                nums[i] += (i>>j) &1;
            }
        }
        return nums;
    }
}

posted @ 2022-02-03 16:52  蹇爱黄  阅读(24)  评论(0)    收藏  举报