Typical way to solve this problem JAVA


  • 0
    A

    Get every bit of the two integer to see if they are the same.

    public class Solution {
        public int hammingDistance(int x, int y) {
            int count = 0;
            for (int i = 0; i < 32; i++) {
                int mask = (1 << i);
                if ((x & mask) != (y & mask)) count++;
            }
            return count;
        }
    }
    

Log in to reply
 

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