Simple Golang Solution

  • 0

    First, Convert the corresponding XOR to a binary string (Sprintf returns the value of as string instead of printing to a stream)
    Then, iterate over the string and increment diff if the i-th digit in bin is 1.

    import "fmt"
    func hammingDistance(x int, y int) int {
        bin, diff := fmt.Sprintf("%b",x^y), 0
        for i:=0; i<len(bin);i++{
            if string(bin[i]) == "1"{
        return diff

Log in to reply

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