LeetCode *** 338. Counting Bits
来源:互联网 发布:暴漫官网淘宝 编辑:程序博客网 时间:2024/03/29 10:36
题目:
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 timeO(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
表2:
十进制二进制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得到,当数字从偶数变为奇数时,1的个数会增加一个。
从表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; }};
0 0
- [leetcode] 338. Counting Bits
- leetcode 338. Counting Bits
- [LeetCode]338. Counting Bits
- LeetCode#338. Counting Bits
- [LeetCode] 338. Counting Bits
- LeetCode 338. Counting Bits
- [LeetCode]338. Counting Bits
- LeetCode-338. Counting Bits
- LeetCode *** 338. Counting Bits
- (leetcode) 338. Counting Bits
- #leetcode#338. Counting Bits
- LeetCode 338. Counting Bits
- leetcode 338. Counting Bits
- LeetCode-338. Counting Bits
- LeetCode-338. Counting Bits
- leetcode-338. Counting Bits
- Leetcode 338. Counting Bits
- LeetCode 338. Counting Bits
- Spring MVC整合fastjson
- #define private public
- Nginx+Keepalived实现站点高可用
- 码农小汪-剑指Offer之34 -两个链表的第一个公共结点 hashMap
- Android 修改阿拉伯语数字显示
- LeetCode *** 338. Counting Bits
- bzoj4404 Binary vs Decimal
- Node.js模板引擎: Jade入门
- WifiDog 认证原理和流程
- decltype(表达式)
- Spring MVC系列(五)之自定义数据绑定---HandlerMethodArgumentResolver
- ubuntu+hadoop2.6.0+集群模式
- SOLID五大 原则(腾讯2017实习生题目)
- MyBatis知识系列之三:MyBatis的配置文件:mybatis.cfg.xml和xxx.mapper.xml文件的解析