Straightforward Java masking solution 14ms


  • 0
    M

    Implementation of "Bit Count".

    public class Solution {
        public int hammingDistance(int x, int y) {
            int hd = 0;
            while (x != 0 || y != 0) {
                if ((x & 1) != (y & 1)) {
                    hd++;
                }
                x >>= 1;
                y >>= 1;
            }
            
            return hd;
        }
    }
    

Log in to reply
 

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