(LeetCode) 338. Counting Bits
来源:互联网 发布:淘宝运营助手在哪里 编辑:程序博客网 时间:2024/06/15 14:00
338. 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.
代码
class Solution {public: vector<int> countBits(int num) { vector<int> ret(num+1, 0); for (int i = 1; i <= num; ++i) ret[i] = ret[i&(i-1)] + 1; return ret; }};
阅读全文
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
- fl2440-添加RT3070驱动(STA模式)实现无线上网
- ajxp 查询添加元素
- 常用的几个网站
- bzoj 1185 [HNOI2007]最小矩形覆盖(坑)
- 多态草稿
- (LeetCode) 338. Counting Bits
- STL库学习 参考大道至简(更新完毕)
- CyclicBarrier
- javascript:touch事件
- 持久化存储技术之SharedPreferences存储
- D3 GEO应用专题(一):绘制旋转的3D地球
- 搜索 J
- 第二十二天 http状态管理技术-cookies和session
- 机器学习adaboost实现