Python 1 Line Beats 100% Solution


  • 0
    M
    def hammingDistance(self, x, y):        
        return list(bin(x ^ y)[2:]).count('1')
    

    Brief explanation: bin returns a string that starts with 0b (hence the [2:]).


  • 0
    A

    @martingale
    Good solution, but [2:] is not necessary, as we only care "1".
    def hammingDistance(self, x, y):
    return list(bin(x^y)).count("1")


  • 0

    a similar version

    class Solution(object):
        def hammingDistance(self, x, y):
            """
            :type x: int
            :type y: int
            :rtype: int
            """
            return bin(x^y).count("1") 
    

  • 0
    M

    In my opinion 'list()' is not necessary.
    """
    return bin(x^y).count('1')
    """


Log in to reply
 

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