LeetCode338. Counting Bits (2 solutions)

Posted 陆草纯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode338. Counting Bits (2 solutions)相关的知识,希望对你有一定的参考价值。

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.

 

Show Hint 

    Credits:
    Special thanks to @ syedee for adding this problem and creating all test cases.

     

    解法一:

    按照定义做,注意,x&(x-1)可以消去最右边的1

    class Solution {
    public:
        vector<int> countBits(int num) {
            vector<int> ret;
            for(int i = 0; i <= num; i ++)
                ret.push_back(countbit(i));
            return ret;
        }
        int countbit(int i)
        {
            int count = 0;
            while(i)
            {
                i &= (i-1);
                count ++;
            }
            return count;
        }
    };

    技术分享

     

    解法二:

    对于[2^k, 2^(k+1)-1]区间,可以划分成前后两部分

    前半部分与[2^(k-1), 2^k-1]内的值相同,后半部分与[2^(k-1), 2^k-1]内值+1相同。

    class Solution {
    public:
        vector<int> countBits(int num) {
            vector<int> ret;
            
            ret.push_back(0);
            if(num == 0)
            // start case 1
                return ret;
            ret.push_back(1);
            
            if(num == 1)
            // start case 2
                return ret;
    
            // general case
            else
            {
                int k = 0;
                int result = 1;
                while(result-1 < num)
                {
                    result *= 2;
                    k ++;
                }
                // to here, num∈ [2^(k-1),2^k-1]
                int gap = pow(2.0, k) - 1 - num;
                for(int i = 0; i < k-2; i ++)
                {// infer from [2^i, 2^(i+1)-1] to [2^(i+1), 2^(i+2)-1]
                    // copy part
                    for(int j = pow(2.0, i); j <= pow(2.0, i+1)-1; j ++)
                        ret.push_back(ret[j]);
                    // plus 1 part
                    for(int j = pow(2.0, i); j <= pow(2.0, i+1)-1; j ++)
                        ret.push_back(ret[j]+1);
                }
                for(int i = 0; i < pow(2.0, k-1) - gap; i ++)
                {
                    int j = pow(2.0, k-2) + i;
                    if(i < pow(2.0, k-2))
                    // copy part
                        ret.push_back(ret[j]);
                    else
                    // plus 1 part
                        ret.push_back(ret[j]+1);
                }
            }
            return ret;
        }
    };

    技术分享

     

    以上是关于LeetCode338. Counting Bits (2 solutions)的主要内容,如果未能解决你的问题,请参考以下文章

    leetcode 338. Counting Bits

    [leetcode-338-Counting Bits]

    leetcode [338]Counting Bits

    LeetCode 338. Counting Bits

    LeetCode338. Counting Bits (2 solutions)

    LeetCode 338. Counting Bits