【LeetCode】338. Counting Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.

Example:
For num = 5 you should return [0,1,1,2,1,2].

Follow up:
It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
Space complexity should be O(n).
Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.

题目:
给定一个非负整数num。对于每一个数i,i在0到num的范围内,计算每一个数二进制形式下的1的个数,并以数组的形式返回输出。
时间复杂度和空间复杂度应该都为O(n)。

分析:
先列出一串数字来看看规律:
0 0000 0个
1 0001 1个
2 0010 1个
3 0011 2个
4 0100 1个
5 0101 2个
6 0110 2个
7 0111 3个
8 1000 1个
num=0时,结果为[0];
num=1时,结果为[0, 1],该结果的第二个值为第一个的值加1;
num=2时,结果为[0, 1, 1];
num=3时,结果为[0, 1, 1, 2],该结果的第三、四个值为前两个的值加1;
num=4时,结果为[0, 1, 1, 2, 1];
num=5时,结果为[0, 1, 1, 2, 1, 2];
num=6时,结果为[0, 1, 1, 2, 1, 2, 2];
num=7时,结果为[0, 1, 1, 2, 1, 2, 2, 3],该结果的后四个的值为前四个的值加1;
num=8时,结果为[0, 1, 1, 2, 1, 2, 2, 3, 1];
可以发现,每一次加1都是以2的倍数为界限,
对于2^N个数而言,后2^(N-1)个数的重复规律,正好等于前2^(N-1)个数的重复规律再加1。

/**
 * @param {number} num
 * @return {number[]}
 */
var countBits = function(num) {
    var result = [];
    result[0] = 0;
    var N = 1;
    while (N <= num) {
        let next = 2 * N;
        for (let i = N; i < next && i <= num; i++) {
            result[i] = result[i-N] + 1;
        }
        N = next;
    }
    return result;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值