Java solution easy to understand


  • 0
    Z
    public int hammingDistance(int x, int y) {
            int diffInt = x ^ y;
            int count = 0;
            while (diffInt >= 1) {
                //Integer to bit
                count += diffInt % 2 == 1 ? 1 : 0;
                diffInt /= 2;
            }
            return count;
     }
    

Log in to reply
 

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