Python with/without builtin


  • 0
    T

    with bin()

    class Solution(object):
        def countBits(self, num):
            """
            :type num: int
            :rtype: List[int]
            """
            res=[]
            for i in range(num+1):
            	res.append(bin(i).count('1'))
            return res
    

    iteration

    class Solution(object):
        def countBits(self, num):
            """
            :type num: int
            :rtype: List[int]
            """
            res=[]
            for i in range(num+1):
            	c=0
            	while i>0:
            		c+=i%2
            		i/=2
            	res.append(c)
            return res
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.