My easy Python solution

  • 0
    class Solution(object):
        def hammingDistance(self, x, y):
            :type x: int
            :type y: int
            :rtype: int
            bin_x = str(bin(x))[2:]
            bin_y = str(bin(y))[2:]
            if len(bin_x) > len(bin_y):
                temp = bin_x
                bin_x = bin_y
                bin_y = temp
            bin_x = bin_x.zfill(len(bin_y))
            count = 0
            for x_elem, y_elem in zip(bin_x, bin_y):
                if not x_elem == y_elem:
                    count += 1
            return count

Log in to reply

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