normal Java solution with mod operation


  • 0
    Y

    A normal solution if you don't want to use bit tricks and system function.

    public int hammingDistance(int x, int y) {
            int num = (x ^ y);
            int count = 0;
            while (num > 0) {
                if (num % 2 != 0) {
                    count++;
                }
                num = num / 2;
            }
            return count;
        }
    

Log in to reply
 

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