Wrong test case?


  • 0
    M

    Here is my c# solution:

    public IList<int> GrayCode(int n) {
    
        var output = new List<int>();
    
        int num = 0;
        output.Add(num);
        
        if(n == 0)
            return output;
        
        if(n == 1)
        {
            output.Add(1);
            return output;
        }
        
        int count = 0;
        int bit = 1;
        bool left = true;
        int nums = 1 << n;
        
        while(true){
            for(int i = 0; i < n - 1; i++){
                if(count++ == (nums - 1))
                    return output;
    
                num ^= bit;
                output.Add(num);
                
                if(left)
                    bit = bit << 1;
                else
                    bit = bit >> 1;
            }
            left = !left;
        }
        
        return null;
    }
    

    The test case for input 3 fails:

    Output: [0,1,3,7,5,4,6,2]
    Expected: [0,1,3,2,6,7,5,4]

    Is the test case incorrect?


Log in to reply
 

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