Counting Bits

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.

Show Hint

Credits:
Special thanks to @ syedeefor adding this problem and creating all test cases.

解法一:

按照定义做,注意,x&(x-1)可以消去最右边的1

class Solution {
public:
vector<int> countBits(int num) {
vector<int> ret;
for(int i = ; i <= num; i ++)
ret.push_back(countbit(i));
return ret;
}
int countbit(int i)
{
int count = ;
while(i)
{
i &= (i-);
count ++;
}
return count;
}
};

解法二:

对于[2^k, 2^(k+1)-1]区间,可以划分成前后两部分

前半部分与[2^(k-1), 2^k-1]内的值相同,后半部分与[2^(k-1), 2^k-1]内值+1相同。

class Solution {
public:
vector<int> countBits(int num) {
vector<int> ret; ret.push_back();
if(num == )
// start case 1
return ret;
ret.push_back(); if(num == )
// start case 2
return ret; // general case
else
{
int k = ;
int result = ;
while(result- < num)
{
result *= ;
k ++;
}
// to here, num∈ [2^(k-1),2^k-1]
int gap = pow(2.0, k) - - num;
for(int i = ; i < k-; i ++)
{// infer from [2^i, 2^(i+1)-1] to [2^(i+1), 2^(i+2)-1]
// copy part
for(int j = pow(2.0, i); j <= pow(2.0, i+)-; j ++)
ret.push_back(ret[j]);
// plus 1 part
for(int j = pow(2.0, i); j <= pow(2.0, i+)-; j ++)
ret.push_back(ret[j]+);
}
for(int i = ; i < pow(2.0, k-) - gap; i ++)
{
int j = pow(2.0, k-) + i;
if(i < pow(2.0, k-))
// copy part
ret.push_back(ret[j]);
else
// plus 1 part
ret.push_back(ret[j]+);
}
}
return ret;
}
};

 

【LeetCode】338. Counting Bits (2 solutions)的更多相关文章

  1. 【leetcode】338 .Counting Bits

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

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

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

  3. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  4. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  5. 【LeetCode】190. Reverse Bits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...

  6. 【LeetCode】190. Reverse Bits

    题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  7. 【Leetcode】338. Bit位计数

    每次刷leetcode都有一种发现新大陆的感觉. 题目链接:https://leetcode-cn.com/problems/counting-bits/description/ 给定一个非负整数 n ...

  8. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  9. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

随机推荐

  1. Java截图笔记

  2. Oracle中改变表的Owner和tablespace

    初用Oracle,很多的不熟悉,建完库,没有建用户,也没创建表空间,就直接system用户建表添加数据,几个月过去,表建了近百个,数据添加了几万条,才越来越觉得这种方式缺点太多: 在PL/SQL中系统 ...

  3. jsp_数据库的连接

    一.添加数据库以及表 在这里我们使用的是mysql数据库 二.配置数据库的驱动程序 将mysql的驱动程序复制到Tomcat目录下的lib目录中 注:在Tomcat中如果配置了新的jar包,则配置完成 ...

  4. border-radius(边框半径)

    可以使用像素来指定border-radius的属性值使角变圆 除了像素,你还可以使用百分比来指定border-radius边框半径的值

  5. SQLLDR记录数与文本记录数比较

    我们平时都用sqlldr进行将文本数据加载到数据库,但是有时候由于数据问题导致入库率不能达到100%,因此我们要检测是否存在不能入库的数据记录.以下shell脚本就是统计文本中记录数和数据库中记录数是 ...

  6. 时隔两年最近再次折腾opensuse 的一些笔记 - opensuse linux java service shell

    时隔两年最近再次折腾opensuse 的一些笔记 - opensuse linux java service shell opensuse 一些常用命令:    service xxx start/s ...

  7. 史无前例的Firefox奇怪问题:host中的common名称造成css文件无法加载

    今天遭遇了一个非常非常奇怪的问题,一个css文件(common.cnblogs.com/Skins/marvin3/green.css),Firefox怎么也无法打开,一直在转圈. 而换成其它浏览器都 ...

  8. Python模拟HTTP Post上传文件

    使用urllib2模块构造http post数据结构,提交有文件的表单(multipart/form-data),本示例提交的post表单带有两个参数及一张图片,代码如下: #buld post bo ...

  9. [游戏模版18] Win32 五子棋

    >_<:Learning its AI logic. >_<:resource >_<:code: #include <windows.h> // C ...

  10. 无线客户端框架设计(5.1):将JSON映射为实体对象(iOS篇)

    iOS开发人员已经习惯于将JSON转换为字典或者数组来进行操作了,接下来我要做的事情,可能匪夷所思,但是,对WP和Android开发人员而言,他们更倾向于将JSON转换为实体对象进行操作. 我所设计的 ...