Share my C++ solution, easy to understand


  • 0
    V
    class Solution {
    public:
        int hammingDistance(int x, int y) {
            int ret = 0;
            int diff = 0;
            
            diff = x ^ y;
            
            while (diff != 0)
            {
                ret++;
                diff &= diff - 1;
            }
            
            return ret;
        }
    };class Solution {
    public:
        int hammingDistance(int x, int y) {
            int ret = 0;
            int diff = 0;
            
            diff = x ^ y;
            
            while (diff != 0)
            {
                ret++;
                diff &= diff - 1;
            }
            
            return ret;
        }
    };
    

Log in to reply
 

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