Java solution without XORing


  • 0
    O
    public class Solution {
        public int hammingDistance(int x, int y) {
            int number=x>y?x:y;
            int count = 0;
            while(number>0){
                int bin = x%2;
                x/=2;
                int bin2 =y%2;
                y/=2;
                if(bin!=bin2) count++;
                number/=2;
            }
            return count;
        }
    }
    
    

Log in to reply
 

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