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.
Credits:
Special thanks to @ syedee for adding this problem and creating all test cases.
给定一个非负整数 num。 对于范围 0 ≤ i ≤ num 中的每个数字 i ,计算其二进制数中的1的数目并将它们作为数组返回。
示例:
比如给定 num = 5
,应该返回 [0,1,1,2,1,2]
.
进阶:
- 给出时间复杂度为O(n * sizeof(integer)) 的解答非常容易。 但是你可以在线性时间O(n)内用一次遍历做到吗?
- 要求算法的空间复杂度为O(n)。
- 你能进一步完善解法吗? 在c ++或任何其他语言中不使用任何内置函数(如c++里的 __builtin_popcount)来执行此操作。
致谢:
特别感谢 @syedee 添加此问题及所有测试用例。
本题,首先根据题目要求,我们不考虑c++里的 __builtin_popcount进行直接计算。
计算其二进制数中的1的数目,我们首先写出部分非负整数并转化为二进制形式,来发现其中的规律。
0 0000 0 1 0001 1 2 0010 1 3 0011 2 4 0100 1 5 0101 2 6 0110 2 7 0111 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
根据写出的数据,我们可以看到两个较为明显的规律,从而得出两个比较容易的方法
方法一:我们首先可以看到每个i值都是i&(i-1)对应的值加1,例:4的个数是4&3的值的个数再加1,关于与(&)操作,可以查询网上相关资料,代码如下:
1 class Solution { 2 public: 3 vector<int> countBits(int num) { 4 vector<int> res; 5 for(int i=1;i<=num;++i) 6 { 7 res[i]=res[i&(i-1)]+1; 8 } 9 return res; 10 } 11 };
方法二:我们可以看出奇数的1的个数是该数除以2后得到的数的1的个数加1,例:3的对应的1的个数是3/2的个数+1即1+1=2,代码如下:
class Solution { public: vector<int> countBits(int num) { vector<int> res{0}; for (int i = 1; i <= num; ++i) { if (i % 2 == 0) res.push_back(res[i / 2]); else res.push_back(res[i / 2] + 1); } return res; } };