Hamming Distance Java Solution


  • 0
    R
    class Solution {
        public int hammingDistance(int x, int y) {
            int count = 0;
            for(int i=0; i<String.format("%32s", Integer.toBinaryString(x)).replace(" ", "0").length(); i++){
             if(String.format("%32s", Integer.toBinaryString(x)).replace(" ", "0").toCharArray()[i]!=
                String.format("%32s", Integer.toBinaryString(y)).replace(" ", "0").toCharArray()[i]){
                 count++;
             }   
            }
            return count;
        }
    }
    

Log in to reply
 

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