My python solution using recursion


  • 0
    C
    def hammingWeight(self, n):
        """
        :type n: int
        :rtype: int
        """
        return 0 if n == 0 else (1 & n) + self.hammingWeight(n>>1)

Log in to reply
 

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