2.Counting Bits
Example 1:
Input: 2
Output: [0,1,1]
Example 2:
Input: 5
Output: [0,1,1,2,1,2]Solution I : (Using STL)
class Solution
{
public:
vector<int> countBits(int num)
{
vector<int> v;
for (int i = 0; i <= num; i++)
{
bitset<32> b(i);
int x = b.count();
v.push_back(x);
}
return v;
}
};Solution II:(Brian Kernighan’s Algorithm)
Solution III: (Using property of even odd no and division)
Last updated