[Medium]Counting Bits
来源:互联网 发布:socket异步收发数据 编辑:程序博客网 时间:2024/05/16 17:25
问题:
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> res; int n = 2; res.clear(); res.push_back(0); res.push_back(1); while (n - 1 < num) { for (int i = 0; i < n; ++i) { res.push_back(res[i] + 1); } n = n * 2; } res.resize(num + 1); return res; }};
0 0
- 338. Counting Bits #Medium
- [Medium] Counting Bits
- [Medium]Counting Bits
- 338. Counting Bits -Medium
- 338. Counting Bits-medium
- 338. Counting Bits [medium] (Python)
- Medium 338题 Counting Bits
- leetcode338:Counting Bits(medium)
- 338. Counting Bits 难度:medium
- Leetcode #338 - Counting bits - Medium
- LeetCode 338. Counting Bits (Medium)
- 338.LeetCode Counting Bits(medium)[二进制位处理]
- Leetcode 338. Counting Bits (Medium) (cpp)
- Counting Bits——Difficulty:Medium
- LeetCode解题报告 338. Counting Bits [medium]
- 【leetcode】【Medium】【338. Counting Bits】【规律】
- Leet Code OJ 338. Counting Bits [Difficulty: Medium]
- 动态规划——338. Counting Bits[Medium]
- [leetcode-3]Longest Substring Without Repeating Characters
- 紫书 例题7-2 UVA 11059 暴力枚举
- [Medium]Integer Break
- 图像特征提取三大法宝:HOG特征,LBP特征,Haar特征
- static类成员使用注意
- [Medium]Counting Bits
- 紫书 例题7-3 UVA 10976
- 工业网络空间安全态势分析报告
- python windows扩展包
- Window 7下 MongoDB 的安装应用简析
- Android实现动画效果之淡入淡出
- [Medium]Coin Change
- Linux安装Mongodb
- c语言学习笔记(标准文件io)