Leetcode 477. Total Hamming Distance

按位计算就行.

class Solution(object):
    def totalHammingDistance(self, nums):
        size, ans = len(nums), 0
        while True:
            zero= 0
            count=[0,0]
            for i in range(size):
                if nums[i] == 0:
                    zero += 1
                count[nums[i] % 2] += 1
                nums[i] >>= 1
            ans += count[0] * count[1]
            if zero == size:
                return ans

 

posted @ 2019-03-23 06:11  周洋  阅读(129)  评论(0编辑  收藏  举报