Hamming Distance Solution C++ Brief


  • 0
    H

    in the problem x,y<2^31, it needs 31 times to calculate.

    class Solution {
    public:
        int hammingDistance(int x, int y) {
           int z=x^y;
           int count=0;
           for (int i=0;i<31;i++)
           {
             if (z&1)count++;
             z>>=1;
           }  
           return count; 
        }
    };
    

Log in to reply
 

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