Using Brian Kernighan’s Algorithm


  • 0
    public class Solution {
        public int hammingDistance(int x, int y) {
            
            int res = x ^ y, count = 0;
            while( res > 0 ) {
                res = res & ( res - 1 );
                count++;
            }
            return count;
        }
    }
    

Log in to reply
 

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