Every time the number of digit increased by 1, we start from beginning and add 1.

```
def countBits(self, num):
"""
:type num: int
:rtype: List[int]
"""
d = 1
result = [0]
j = 0
for i in xrange(num):
result.append(result[j]+1)
if j+1 == d:
j = 0
d *= 2
else:
j += 1
return result```
```