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.

Hint:

  1. You should make use of what you have produced already.Show More Hint
  2. Divide the numbers in ranges like [2-3], [4-7], [8-15] and so on. And try to generate new range from previous.Show More Hint
  3. Or does the odd/even status of the number help you in calculating the number of 1s?

题目大意:

给定一个非负整数num。对于每一个满足0 ≤ i ≤ num的数字i,计算其数字的二进制表示中1的个数,并以数组形式返回。

测试用例如题目描述。

进一步思考:

很容易想到运行时间 O(n*sizeof(integer)) 的解法。但你可以用线性时间O(n)的一趟算法完成吗?

空间复杂度应当为O(n)。

你可以像老板那样吗?不要使用任何内建函数(比如C++的__builtin_popcount)。

提示:

  1. 你应当利用已经生成的结果。
  2. 将数字拆分为诸如 [2-3], [4-7], [8-15] 之类的范围。并且尝试根据已经生成的范围产生新的范围。
  3. 数字的奇偶性可以帮助你计算1的个数吗?

解法一:

给定num,对于每个 0<= i <=num , 计算i的2进制中1的个数。

若对每个数,进行每位的判断,时间复杂度是O(n*logn)。

可以从每个数的最低位开始分析,例如1001001 ,它的二进制1的个数等于100100 总二进制个数 + 1 。即 f = f/2 + (f 的最低位)

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

解法二:按位与。i & (i-1) 清除了i中最右边的1.

v[i] = v[i & (i-1)] + 1;

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

解法三:可以从n的最高位入手,

递推式:v[n] = v[n - highbits(n)] + 1

其中highbits(n)表示只保留n的最高位得到的数字。

Leetcode 338. Counting Bits的更多相关文章

  1. LN : leetcode 338 Counting Bits

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

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

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

  3. Java [Leetcode 338]Counting Bits

    题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...

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

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

  6. 【LeetCode】Counting Bits(338)

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

  7. 【leetcode】338 .Counting Bits

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

  8. 【LeetCode】338. Counting Bits 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目描述 Given a non negati ...

  9. 338. Counting Bits

    https://leetcode.com/problems/counting-bits/ 给定一个非负数n,输出[0,n]区间内所有数的二进制形式中含1的个数 Example: For num = 5 ...

随机推荐

  1. #maven系列(4)-maven插件的介绍

    1. 简介 在Maven设计中,实际的任务都是交由插件完成的,这种思想和设计模式中的模板方法非常类似,模板方法模式在父类中定义算法的整体结构,子类可以通过实现或者重写父类的方法来控制实际的行为,这样既 ...

  2. 在C++中定义常量的两种方法的比较

    常量是定以后,在程序运行中不能被改变的标识符.C++中定义常量可以用#define .const 这两种方法.例如:#define PRICE 10 //定义单价常量10const int PRICE ...

  3. 使用paramiko进行打包操作

    使用paramiko执行ssh命令的时候有一个很坑爹的地方:它无法准确的识别你的系统环境变量,所以使用一些命令的时候会发现,直接在系统中执行该命令的时候可以,但是换成paramiko执行的时候会报错说 ...

  4. POJ 3694 Network (tarjan + LCA)

    题目链接:http://poj.org/problem?id=3694 题意是给你一个无向图n个点,m条边,将m条边连接起来之后形成一个图,有Q个询问,问将u和v连接起来后图中还有多少个桥. 首先用t ...

  5. c语言向文件中写入

    创建一个文件使用fopen打开,然后使用fprintf输出,最后关闭文件流 FILE *out; out = fopen("test.txt","a+"); i ...

  6. 浅谈TCP优化

    原文地址:http://kb.cnblogs.com/page/197406/ 很多人常常对TCP优化有一种雾里看花的感觉,实际上只要理解了TCP的运行方式就能掀开它的神秘面纱.Ilya Grigor ...

  7. Xdebug的使用

    1.http://www.cnblogs.com/mo-beifeng/articles/2446142.html 2.http://www.cnblogs.com/ximu/articles/200 ...

  8. iterm2相关配置

    使用 iterm2 登陆 公司堡垒机 进行 上传 下载文件 等维护操作.. 1.需要安装iterm2 软件 http://iterm2.com/  下载安装 2.安装brew ruby -e &quo ...

  9. c#加密 可逆与不可逆MD5 加密

    1.方法一 (不可逆加密) srxljl public string EncryptPassword(string PasswordString,string PasswordFormat )     ...

  10. 飘逸的python - 编码杂症之在字符串前面加u

      有时候我们从其它地方接受的字符串经过艰难跋涉,它变了个样.比如收到的是'\u6253\u602a\u8005'而不是u'\u6253\u602a\u8005'. 明明肉眼看起来只需要加个u,但是怎 ...