LeetCode *** 338. Counting Bits
2016-04-05 16:17
381 查看
题目:
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
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.
分析:
输出从0到num用二进制表示时1的个数,且要求时间复杂度为O(n),空间复杂度同样为O(n)。
那么大概可以知道这是一个找规律的题目,那就列个表出来看看吧:
表1:
表2:
从表1得到,当数字从偶数变为奇数时,1的个数会增加一个。
从表2可以发现数字从奇数变为偶数时,橘色部分的末两位都是由01变为10,且1的个数不变,而黑色部分末两位都是由11变为00,且1的个数普遍会减少。此时的该偶数的1的个数与该偶数/4的那个数一样多。。。。但是写到这里我发现偶数会和该偶数/2的个数一样多。。。于是代码如下。。
代码:
一个更简洁的:
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 = 5you 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.
分析:
输出从0到num用二进制表示时1的个数,且要求时间复杂度为O(n),空间复杂度同样为O(n)。
那么大概可以知道这是一个找规律的题目,那就列个表出来看看吧:
表1:
十进制 | 二进制 | 1的个数 |
0 | 0 | 0 |
1 | 1 | 1 |
2 | 10 | 1 |
3 | 11 | 2 |
4 | 100 | 1 |
5 | 101 | 2 |
6 | 110 | 2 |
7 | 111 | 3 |
8 | 1000 | 1 |
9 | 1001 | 2 |
10 | 1010 | 2 |
11 | 1011 | 3 |
12 | 1100 | 2 |
13 | 1101 | 3 |
14 | 1110 | 3 |
15 | 1111 | 4 |
16 | 10000 | 1 |
17 | 10001 | 2 |
18 | 10010 | 2 |
19 | 10011 | 3 |
20 | 10100 | 2 |
21 | 10101 | 3 |
22 | 10110 | 3 |
23 | 10111 | 4 |
24 | 11000 | 2 |
25 | 11001 | 3 |
26 | 11010 | 3 |
27 | 11011 | 4 |
28 | 11100 | 3 |
29 | 11101 | 4 |
30 | 11110 | 4 |
31 | 11111 | 5 |
32 | 100000 | 1 |
十进制 | 二进制 | 1的个数 |
0 | 0 | 0 |
1 | 1 | 1 |
2 | 10 | 1 |
3 | 11 | 2 |
4 | 100 | 1 |
5 | 101 | 2 |
6 | 110 | 2 |
7 | 111 | 3 |
8 | 1000 | 1 |
9 | 1001 | 2 |
10 | 1010 | 2 |
11 | 1011 | 3 |
12 | 1100 | 2 |
13 | 1101 | 3 |
14 | 1110 | 3 |
15 | 1111 | 4 |
16 | 10000 | 1 |
17 | 10001 | 2 |
18 | 10010 | 2 |
19 | 10011 | 3 |
20 | 10100 | 2 |
21 | 10101 | 3 |
22 | 10110 | 3 |
23 | 10111 | 4 |
24 | 11000 | 2 |
25 | 11001 | 3 |
26 | 11010 | 3 |
27 | 11011 | 4 |
28 | 11100 | 3 |
29 | 11101 | 4 |
30 | 11110 | 4 |
31 | 11111 | 5 |
32 | 100000 | [align=right]1[/align] |
从表2可以发现数字从奇数变为偶数时,橘色部分的末两位都是由01变为10,且1的个数不变,而黑色部分末两位都是由11变为00,且1的个数普遍会减少。此时的该偶数的1的个数与该偶数/4的那个数一样多。。。。但是写到这里我发现偶数会和该偶数/2的个数一样多。。。于是代码如下。。
代码:
class Solution { public: vector<int> countBits(int num) { vector<int> res; res.push_back(0); for(int i=1;i<=num;++i){ int count=res.at(res.size()-1); if(i%2)res.push_back(count+1); else{ if(!((i-2)%4))res.push_back(count); else res.push_back(res.at(i/4)); } } return res; } };
一个更简洁的:
class Solution { public: vector<int> countBits(int num) { vector<int> res; res.push_back(0); for(int i=1;i<=num;++i){ if(i%2)res.push_back(res.at(res.size()-1)+1); else res.push_back(res.at(i/2)); } return res; } };
相关文章推荐
- HDU 4148 Length of S(n)(兴爷版)
- HDOJ 1072 Nightmare
- 单机数据库更改数据文件redo控制文件的位置
- java中的Object...是什么意思详解。
- java的下拉框选择
- Android ListView相关技术
- Poj 2236 Wireless Network【并查集】
- 模仿美团点评的Android应用中价格和购买栏悬浮固定的效果
- 基于动态时间规整算法(DTW)的相似度计算
- android加载html乱码解决办法
- 办公大揭秘:pdf怎么转word
- windows下sublime text3下载注册及汉化
- 15.编写LED程序及反汇编工具
- 程序启动读取和关闭时保存应用程序设置(QSettings)
- delphi FillChar的用法(转)
- Magicodes.WeiChat——WeChatOAuthTest(网页授权获取用户基本信息)
- JS中的forEach、$.each、map方法推荐
- Bzoj1591:[Usaco2008 Dec]Largest Fence 最大的围栏:DP
- 会话标识未更新问题
- 如何让按钮UIButton的文字左对齐?