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. URAL 2037 Richness of binary words (回文子串,找规律)

    Richness of binary words 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/B Description Fo ...

  2. Spring Object/XML mapping example

    In this tutorial, we will extend last Maven + Spring hello world example by adding JDBC support, to ...

  3. hdu 4496 (并差集)

    题意:给出一个图,m条边,输出删除前i条边后该图的联通块的个数. 思路:刚开始想着是不是联通问题,后来看明白题意后知道,如果从最后一条边添加的话,答案就会出来了,就是并差集的操作. #include& ...

  4. UVaLive 7370 Classy (排序,比较)

    题意:给定 n 个人,和他们的数进行比较,从后面开始比,如果不够长,加middle接着比,直到没有,如果还相同比名字. 析:很水的题,就不用说了. 代码如下: #pragma comment(link ...

  5. animate平滑回到顶部

    Js: //回到顶部 $(".totop").click(function () { $("body,html").animate({scrollTop: 0} ...

  6. C++中标准容器Vector,元素操作.insert()小结

    insert() 函数有以下三种用法: iterator insert( iterator loc, const TYPE &val );  //在指定位置loc前插入值为val的元素,返回指 ...

  7. Myeclipse 10.x 安装Aptana3.2 插件

    安装步骤: 1.下载aptana3.2 Eclipse Plugin插件. 下载地址:http://update1.aptana.org/studio/3.2/024747/index.html 2. ...

  8. 前端响应式设计中@media等的相关运用

    现在做前端响应式网站特别,响应式成为现在前端设计一个热点,它成为热点的最主要的原因就是,移动端设备屏幕的种类多样,那么如何设置响应式屏幕. /*打印样式*/ @mediaprint{color:red ...

  9. CentOS 6.3(x86_64)下安装Oracle 10g R2

    目 录 一.硬件要求二.软件三.系统安装注意四.安装Oracle前的系统准备工作五.安装Oracle,并进行相关设置六.升级Oracle到patchset 10.2.0.4七.使用rlwrap调用sq ...

  10. 处理Oracle中杀不掉的锁

    一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长时间不释放,有时实在没办法,只好重启数据库.现在提供一种方法解决这种问题,那就是在ORACLE中杀不 ...