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:
Credits: Special thanks to @ syedee for adding this problem and creating all test cases.
class Solution:
def countBits(self, num):
"""
:type num: int
:rtype: List[int]
"""
ans=[]
for i in range(num+1):
ans.append(int(bin(i).count('1')))
return ans