How to fix the Gray code judge


  • 0
    R

    The following function calculate the bit distance between two integers:

    int bit_diff(int a, int b) {
        
        unsigned c = unsigned(a) ^ unsigned(b);
        
        int result = 0;
        while (c) {
            
            result += (c & 1);
            c >>= 1;
        }
        
        return result;
    }
    

    You can use it to update the judge:

    for ( int i = 0; i < results.size( ) - 1; ++i ) {
    
         if ( bit_diff( results[i], results[i + 1] ) != 1 ) {
    
              return false;
         }
    }
    
    return true;

Log in to reply
 

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