Question

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.

Credits:

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

Solution

动态规划,这里要应用求一个整数,对应二进制数中1的个数的方法,i & (i - 1),也就是将i的最右边的1置为0.

那么每个整数中1的个数,就等于不包含最右边1以后1的个数再加1。 ret[i] = ret[i & (i - 1)] + 1.

Code

class Solution {
public:
vector<int> countBits(int num) {
vector<int> table(num + 1, 0);
for (int i = 1; i <= num; i++) {
table[i] = table[i & (i - 1)] + 1;
}
return table;
}
};

LeetCode——Counting Bits的更多相关文章

  1. LeetCode Counting Bits

    原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...

  2. [LeetCode] Counting Bits 计数位

    Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...

  3. Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits)

    Leetcode之动态规划(DP)专题-338. 比特位计数(Counting Bits) 给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数 ...

  4. 【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  ...

  5. LN : leetcode 338 Counting Bits

    lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...

  6. 2016.5.16——leetcode:Reverse Bits(超详细讲解)

    leetcode:Reverse Bits 本题目收获 移位(<<  >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...

  7. Week 8 - 338.Counting Bits & 413. Arithmetic Slices

    338.Counting Bits - Medium Given a non negative integer number num. For every numbers i in the range ...

  8. leetcode 上的Counting Bits 总结

    最近准备刷 leetcode  做到了一个关于位运算的题记下方法 int cunt = 0; while(temp) { temp = temp&(temp - 1);  //把二进制最左边那 ...

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

    leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...

随机推荐

  1. linux下非root用户的sudo问题

    linux下的root用户是个超级管理员,一般是不用这个用户登录进行操作的,但有时候需要root权限,又不想切换用户的话可以使用sudo命令.但是不是所有的用户都可以使用sudo命令的. 首先可能会遇 ...

  2. 剑指Offer——合并两个排序的链表

    题目描述: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 分析: 苦力活,使用两个指针分别指向未被合并的两个链表的首部,比较两个首部数值的大小,合并数值 ...

  3. Androidstudio中导入内部依赖模块总结

    今天刚从GitHub上找了一个不错的项目,想要把它导入自己的项目中,过程中也遇到了一些小问题,总结一下,以便复习回顾!!!! 1.首先将从GitHub上下载的压缩包进行解压,找到其中的项目文件,直接复 ...

  4. POJO,简单的Java对象

    POJO = "Plain Ordinary Java Object",简单的Java对象,是为了避免和EJB混淆所创造的简称,是MartinFowler等发明的一个术语,用来表示 ...

  5. thunk函数

    1.函数参数求值的策略 a.传值策略(c语言) 传值策略就是在进入函数体之前将 参数计算之后 将参数的值传入到函数体之中. let x = 8 f(x + 1)//参数为 f(9)//传进去的值实际上 ...

  6. CSS 换行知多少: word-wrap && word-break && white-space && word-spacing

    word-wrap : 首先提一下,word-wrap 这个 CSS 属性在CSS3中已经被更名为 overflow-wrap,这样语义化也是为了避免与 word-break 混淆: Referenc ...

  7. Get started on your own KD 8 custom colorway

    The 2009 Summer time Nike Basketball revealed the Cheap KD 8 and revealed three MVP-inspired colors ...

  8. Silly Java-Final 关键字

    Final 关键字 adj. 最终的:最后的:决定性的:不可改变的 1.修饰变量 final variable 意味 [最后的变量,不可改变的变量即常量] Java中该关键字即代表常量 修饰基本类型的 ...

  9. java环境变量及Eclipse自动编译问题

    环境变量,是在操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.例如Windows和DOS操作系统中的path环境变量,当要求系统运行一个程序而没有告诉它程序所在的完整路 ...

  10. 手写ArrayList、LinkedList

    ArrayList package com.hjp.labs; import org.omg.CORBA.PRIVATE_MEMBER; /* 一.ArrayList的底层是Object类的数组,默认 ...