Java Solution


  • 0
    K
    class Solution {
        public int hammingDistance(int x, int y) {
            int firtNumPosition = 0, secondNumPosition = 0;
    		for(int i = 0; (1 << i) <= x || (1 << i) <= y; i++) { //i is the position of the bit
    			if((1 << i) == x)
            	  		firtNumPosition = i;
    			if((1 << i) == y )
    				secondNumPosition = i;
    		}
    		return (secondNumPosition - firtNumPosition);
        }
    }
    

Log in to reply
 

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