use the xor idea


  • 0
    K

    ‘’‘
    public int hammingDistance(int x, int y) {
    int xor=(int)(x^y);
    int dist=0;

    	do{
    		int m=xor%2;
    		if(m==1){
    			dist++;
    		}
    		xor/=2;
    	}while(xor>0);
    	
    	return dist;
    }
    

    ’‘’


Log in to reply
 

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