Why is the result always with an extra '\x00'


  • 0
    C

    My code is right when tested on the visual studio, however it is always report to be wrong with the output string with extra '\x00', for example, when the input is "cbac", the expected result should be "cba", but my result is reported as "bac\x00". I cannot figure out why.

    And my code using is as following:

    class Solution {
    public:
        string removeDuplicateLetters(string s) {
            static int lastInx[26]={0};
            for( int i = 0; i < s.length(); i++ )
            {
                lastInx[s[i]-'a'] = i;
            }
            vector<int>lastInxOrder;
            vector<int>cache;
            int cacheInt;
            int length = 0;
            int cacheL = 0;
            for( int i = 0; i < 26; i++ )
            {
                if( length==0 && lastInx[i]!=0 )
                {
                    lastInxOrder.push_back(lastInx[i]);
                    length++;
                }
                else if( lastInx[i]!=0 )
                {
                    while( length!=0 && lastInx[i] > lastInxOrder[length-1] )
                    {
                        cacheInt = lastInxOrder[length-1];
                        lastInxOrder.pop_back();
                        cache.push_back(cacheInt);
                        length--;
                        cacheL++;
                    }
                    lastInxOrder.push_back(lastInx[i]);
                    length++;
                    while( cacheL!=0 )
                    {
                        cacheInt = cache[cacheL-1];
                        cache.pop_back();
                        lastInxOrder.push_back(cacheInt);
                        length++;
                        cacheL--;
                    }
                    
                }
            }
            
            int startInx,endInx;
            string sret;
            char now;
            
            int nowInx = startInx;
            int count = length;
            
            startInx = 0;
            endInx = lastInxOrder[length-1];
            length--;
            while( count!=0 )
            {
                now = s[startInx];
                nowInx = startInx;
                for( int i = startInx+1;i<=endInx;i++ )
                {
                    if(s[i]<now)
                    {
                        now = s[i];
                        nowInx = i;
                    }
                }
                sret = sret+now;
                startInx = nowInx+1;
                if( nowInx==endInx )
                {
    				if( length!=0 )
    				{
    					endInx = lastInxOrder[length-1];
    					length--;
    				}
    				else
    					break;
                }
                count--;
                
            }
            
            return sret;
        }
    };

  • 0

    The \x00 means your string has a trailing null character. In visual studio you may not be able to see the null character as it's not a printable character.


  • 0
    C

    How can I avoid this? as from the code I did not add the null character in the return string.


  • 0

    You can add this line:

    cout << (int) now << endl;
    

    before this line:

    sret = sret+now;
    

    It will show that you appended a null character.


  • 0
    C

    However there is no null character output even using the 'cout' line, the output is consistent with the expected output, with no null...


Log in to reply
 

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