Skip to content

Instantly share code, notes, and snippets.

@farkwun
Created August 18, 2017 23:12
Show Gist options
  • Save farkwun/b4f13b284e8a261e1977f6fa325ae06c to your computer and use it in GitHub Desktop.
Save farkwun/b4f13b284e8a261e1977f6fa325ae06c to your computer and use it in GitHub Desktop.
477. Total Hamming Distance
# https://leetcode.com/problems/total-hamming-distance/description/
class Solution(object):
def totalHammingDistance(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
i = 0
total_dist = 0
while i < 32:
a = 0
b = 0
for num in nums:
x = num >> i
x = x & 1
if x == 1:
a += 1
else:
b += 1
total_dist += a*b
i += 1
return total_dist
@robin-pham
Copy link

nice simple solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment