Java easy to understand


  • 0
    J

    First I xor the two numbers. Then I count the number of one bits in that number.

    public class Solution {
        public int hammingDistance(int x, int y) {
            int z = x ^ y;
            int count = 0;
            
            while(z>0){
                if(z%2 == 1)
                    ++count;
                z /= 2;
            }
            
            return count;
        }
    }

Log in to reply
 

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