Hamming Distance (SOLUTION C++)


  • 0
     int hammingDistance(int x, int y) {
          string t_X = bitset<31> (x).to_string();
          string t_Y = bitset<31> (y).to_string();
          int count=0;
          if (t_X.length() == t_Y.length())
          {
              for (int i=0; i<t_X.length(); i++)
                   {
                       if (t_X[i] != t_Y[i])
        {
            count += 1;
        }
    }
       } else
          {
              cerr << "ERROR" << endl;
                     }
               return count;   
                    }


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