标签:
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:
Hint:
class Solution(object): def countBits(self, num): a = [0 for i in range(num+1)] for i in range(1,num+1): if i%2 == 0: a[i] = a[i/2] else: a[i] = a[i-1]+1 return a
标签:
原文地址:http://www.cnblogs.com/lilixu/p/5473275.html