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. Android中的 style 和 theme

    通过设置 view 控件的属性,达到设置android UI的目的,如果某些 属性值复用率很高,可以考虑将属性单独声明在 style中,这样就可以达到复用的效果. 一.style Style 概念:A ...

  2. 页面加载异常 清除浏览器静态文件 js css 缓存 js动态加载js css文件,可以配置文件后辍,防止浏览器缓存

    js清除浏览器缓存的几种方法 - 兔老霸夏 - 博客园 https://www.cnblogs.com/Mr-Rocker/p/6031096.html js清除浏览器缓存的几种方法   一.CSS和 ...

  3. 学习IPFS

    注:以下所有操作均在CentOS 6.8 x86_64位系统下完成. IPFS(InterPlanetary File System)是一个点对点的分布式超媒体分发协议,被认为是最有可能取代HTTP的 ...

  4. <2014 10 01> 数学基础 Wikipedia

    数学基础 数学上,数学基础一词有时候用于数学的特定领域,例如数理逻辑,公理化集合论,证明论,模型论,和递归论.但是寻求数学的基础也是数学哲学的中心问题:在什么终极基础上命题可以称为真? 目前占统治地位 ...

  5. Rooks---LightOj1005(排列组合)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1005 题意就是在一个n*n的方格中放k个棋子,每一行每一列都不能有两个棋子,问有多少种 ...

  6. Codeforces441B_Valera and Fruits(暴力)

    Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. 002-java语言基础

    一.安装卸载 卸载:控制面板 安装:下载对应版本 注意1.安装路径→尽量不要有空格和汉字 注意2.安装之后,jre可以不用安装,jdk中含有 二.环境变量 环境变量:理解,一些快捷路径.方便快速查找应 ...

  8. appium 智能滚动

  9. fiddler操作改到本地

    urlreplace test5.api.bookapi.cn:8889 localhost:8080

  10. hdu6194 string string string

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6194 题目: string string string Time Limit: 2000/10 ...