My short solution in python


  • 0
    C

    The idea is to use hash table to get the number of '1'

    class Solution:
        # @param n, an integer
        # @return an integer
        def hammingWeight(self, n):
            b = bin(n)[2:]
            from collections import Counter
            c = Counter(b)  # Counter object is a hash table, values are frequencies of occurrences in the string
            return c['1']

Log in to reply
 

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