338. Counting Bits

xiaoxiao2021-02-28  86

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].

code:

class Solution(object):     def countBits(self, num):         """         :type num: int         :rtype: List[int]         """         res=[]         while num>=0:             res.append(bin(num).count('1'))             num-=1         return res[::-1]

转载请注明原文地址: https://www.6miu.com/read-79746.html

最新回复(0)