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. labview 中activex的初步使用方法

    1.在前面板放置一个activex容器 2.插入activex控件 3.百度找到这个activex控件的属性和方法介绍 4.程序框图中右键activex控件,创建xxx类的方法或者xxx的属性:act ...

  2. POJ3126 Prime Path

    http://poj.org/problem?id=3126 题目大意:给两个数四位数m, n, m的位数各个位改变一位0 —— 9使得改变后的数为素数, 问经过多少次变化使其等于n 如: 10331 ...

  3. js获取服务端IP及端口及协议

    alert("location:"+window.location); alert("href: "+window.location.href); alert( ...

  4. sql的存储过程调用

    USE [ChangHong_612]GO/****** Object: StoredProcedure [dbo].[st_MES_GetCodeRule] Script Date: 09/10/2 ...

  5. SQLServer2008设置开启INTERNET远程连接

    SQL Server 2008默认是不允许远程连接的,sa帐户默认禁用的,如果想要在本地用SSMS连接远程服务器上的SQL Server 2008,需要做两个部分的配置: 使用sa账户登录SQL Se ...

  6. npm package 装包匹配原则

    经常看到package.json 里面有这样的devDependencies: "devDependencies": { "@angular/common": ...

  7. 【多校练习4签到题】HDU 4642—— Fliping game

    来源:点击打开链接 看上去很难,比赛的时候光看hehe了,也没有想. 但是仔细想想,是可以想出来的.一个棋盘上每个格子摆放一个硬币,硬币有正面1和反面0之分.现在两个人可以按照规则翻硬币,选择(x,y ...

  8. Actions 动作集

    --> 移动鼠标到指定位置(先触发onMouseOver动作)        Actions action = new Actions(driver);        WebElement th ...

  9. Android学习第一课

    首先看一个android项目中各个包的作用 以下看几个经常使用的控件: 1. TextView 显示文本框控件 2. EditText 输入文本框 TextView控件经常使用属性: id----控件 ...

  10. WCF 新手教程二

    基本知识: [ServiceContract] Attribute 能够有以下Property 的: CallbackContract 设置callback的类型:Duplicate指Service ...