Dividing number by 2 to get reminder


  • 3
    A

    O(log(n)) solution.

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

Log in to reply
 

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