Just calculate combinations vertically.
class Solution(object): def totalHammingDistance(self, nums): """ :type nums: List[int] :rtype: int """ ans = 0 mask = 1 for j in range(0, 32): ones = zeros = 0 for num in nums: if num & mask: ones += 1 else: zeros += 1 ans += ones * zeros mask = mask << 1 return ans
nice ! but could replace:
for i in xrange(0, len(nums)):
n = len(nums)
for i in xrange(n)
Yeah, that's more elegant and pythonic but a little bit difficult to understand for those who don't use Python.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.