C#, easy to understand solution


  • 0
    B
    public class Solution {
        public int HammingDistance(int x, int y) {
            int count = 0;
            
            while(x != 0 || y != 0) {
                int resultX = x & 1;
                int resultY = y & 1;
                
                if(resultX != resultY) {
                    count++;
                }
                
                x >>= 1;
                y >>= 1;
            }
            
            return count;
        }
    }
    

Log in to reply
 

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