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.

公众号每日一题有详细讲解分析:

P 题的难点在于构建最优子结构。只要构造了最优子结构,一般就比较容易解题了。构造最优子结构的方法有两种:倒推发现 n 与 0 到 n - 1 的关系;使用 DFS 和 递归 方法,从 0 到 n 发现规律。

今天这道题的解法试图通过观察其特点发现规律。这也是一般性的思考问题的方法:通过画图,将抽象的问题具体化,通过具体的例子来发现规律,最后推广到 n 的规模。

class Solution {
public:
    vector<int> countBits(int num) {
        vector<int> res(num + 1, 0);
        res[1] = 1;
        for ( int i = 0; i <= num; i++)
        {
            if ( i % 2 == 0)
            {
                res[i] = res[i/2];
            }
            else
            {
                res[i] = res[i/2] + 1;
            }
        }
        return res;
    }
};
class Solution {
public:
    vector<int> countBits(int num) {
        vector<int> res(num + 1, 0);
        for ( int i = 1; i <= num; i++)
        {
            res[i] = res[i >> 1] + (i & 1);
        }
        return res;
    }
};
时间: 2024-12-27 10:07:45

LeetCode 338. Counting Bits的相关文章

leetCode 338. Counting Bits | Dynamic Programming | Medium

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]. 题目大意

Leetcode 338 – Counting Bits

解法三种: Sln 1. 笨办法,移位.时间复杂度太高,基本就是O(n * sizesof(int)).Sln 2. 利用一个小技巧,偶数除二,在其另外一个乘数左移 1bit而得:奇数在前所得上加1Sln 3. Hamming Weight; 利弊: Sln 1不考虑,复杂度过高.相对来说Sln 2要较Sln 3简单,但是仅仅刷个算法题并没什么特别大的用处,还要考虑一下实际的应用场景.如果我们使用Sln 2,则如果要随机取一个数的二进制1 的个数,需要一个很大的查找表.Sln 3则是随时可用.

leetcode 338. Counting Bits,剑指offer二进制中1的个数

leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种方法,利用奇偶性找规律 class Solution { public: vector<int> countBits(int num) { vector<int> result{0}; for(int i = 1;i <= num;i++){ if(i % 2 == 0) res

leetcode(1)--338.Counting Bits

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,

【LeetCode】338. 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

338. Counting Bits &amp;&amp; 191. Number of 1 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 e

【LeetCode】Counting Bits(338)

1. Description 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]. 2. Answ

338. Counting Bits [medium] (Python)

题目链接 https://leetcode.com/problems/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

【Leetcode】Counting Bits

题目链接:https://leetcode.com/problems/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 y