Java easy 3 lines solution using XOR


  • 0
    T
    public class Solution {
        public int hammingDistance(int x, int y) {
            int count = 0;
            for (char c : Integer.toBinaryString(x^y).toCharArray()) if(c == '1') count ++;
            return count;
        }
    }
    

Log in to reply
 

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