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. 使用adb 查询data/data下的数据库

    1.用cmd打开adb 2.输入adb shell 3.cd到数据库所在目录 4.输入sqlite3 person.db(person.db为要操作的db名称,根据需要修改) 5.输入sql语句(每个 ...

  2. N皇后问题(位运算实现)

    本文参考Matrix67的位运算相关的博文. 顺道列出Matrix67的位运算及其使用技巧 (一) (二) (三) (四),很不错的文章,非常值得一看. 主要就其中的N皇后问题,给出C++位运算实现版 ...

  3. Unity3D研究院之使用Animation编辑器编辑动画(五十四)

     Unity提供了Animation编辑器,它可以为我们编辑物理动画.举个例子比如场景中有一个来回摇动的秋千,这个秋千在项目中完全只起到衬托作用,它不会与别的游戏对象有任何交互.如果这个秋千也用代码来 ...

  4. UITextView

    一.由于IOS中的UITextField不支持文本换行,在需要换行的时候.我们可以用UITextView来解决这一问题.   二.创建步骤 1.初始化并设置位置和大小 UITextView *text ...

  5. ceph placement group状态总结

    一.归置组状态 1. Creating 创建存储池时,它会创建指定数量的归置组.ceph 在创建一或多个归置组时会显示 creating;创建完后,在其归置组的 Acting Set 里的 OSD 将 ...

  6. Caffe学习系列(20):用训练好的caffemodel来进行分类

    caffe程序自带有一张小猫图片,存放路径为caffe根目录下的 examples/images/cat.jpg, 如果我们想用一个训练好的caffemodel来对这张图片进行分类,那该怎么办呢? 如 ...

  7. 加载 CSS 时不影响页面渲染

    转自:http://www.oschina.net/translate/loading-css-without-blocking-render 本文展示了一种技术,它能通过异步下载样式表,以阻止它们的 ...

  8. 教务管理系统数据库E/R图

  9. [外挂8] 自动挂机 SetTimer函数

    >_< : 这里用SetTimer函数自动运行![注意添加在里面的回掉函数函数] UINT SetTimer( HWND hWnd, // 指向窗口句柄 UINT nIDEvent, // ...

  10. BUILD 2015: Visual Studio对GitHub的支持

    微软BUILD 2015大会上发布了Visual Studio 对GitHub的支持.安装了如下Developer Assistant插件后,你便可以在Visual Studio中找到GitHub上的 ...