Need some help here! I got some error, and leetcode judge does not produce my answer.


  • 0
    S

    Hi, here is my solution.
    It works fine on my own machine, but somehow the judge cannot produce the answer.

    """
    /**

    • Function takes the decimal number

    • Function takes the Nth bit (1 to 31)

    • Return the value of Nth bit from decimal
      */
      int nth_bit_of(int decimal, int N)
      {
      // Shifting the 1 for N-1 bits
      int constant = 1 << (N - 1);

      // if the bit is set, return 1
      if ( decimal & constant ) {
      return 1;
      }

      // If the bit is not set, return 0
      return 0;
      }
      /**

    • Helper function that do the real work
      */
      int _hammingDistance(x, y, nth_bit, bit_x, bit_y, hammingDist)
      {
      if (nth_bit >= 32) {
      return hammingDist;
      } else if (nth_bit_of(x, nth_bit) != nth_bit_of(y, nth_bit)) {
      return hammingDistance(x,
      y,
      (nth_bit + 1),
      nth_bit_of(x, (nth_bit + 1)),
      nth_bit_of(y, (nth_bit + 1)),
      (hammingDist + 1));
      } else {
      return hammingDistance(x,
      y,
      (nth_bit + 1),
      nth_bit_of(x, (nth_bit + 1)),
      nth_bit_of(y, (nth_bit + 1)),
      hammingDist);
      }
      }

    // Driver function
    int hammingDistance(int x, int y)
    {
    return _hammingDistance(x, y, 1, nth_bit_of(x, 1), nth_bit_of(y,1), 0);
    }

    """


Log in to reply
 

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