Created
August 18, 2017 23:12
-
-
Save farkwun/b4f13b284e8a261e1977f6fa325ae06c to your computer and use it in GitHub Desktop.
477. Total Hamming Distance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice simple solution