Is this a bug of online judge?


  • 1
    T

    For sample "A", 2. The output of the following code is "A", but can not pass. Is there any problem? Thanks.

    class Solution {

    public:

    string convert(string s, int nRows) {
                
        string result;
        
        string::iterator it=s.begin();
        
        if(nRows==1)
            return s;
        
        for(int i=0; i<nRows; i++)
        {
            bool flag = true;
            it=s.begin()+i;
            
            result+=*it;
            while(it<s.end())
            {
                if(flag)
                {
                    if(i==0 || i==nRows-1)
                        it+=2*(nRows)-2;
                    else
                    {
                        it+=2*(nRows-i)-2;
                        flag=false;
                    }
                }
                else
                {
                    flag=true;
                    it+=2*(i+1)-2;
                }
                
                if(it<s.end())
                    result+=*it;
            }
        }
        
        return result;
    }
    

    };


  • 0
    L

    I have the same issue for "A" test cases. dont know why


  • 1
    L

    I have found the reason. It is due to over bounding of string. You accidentally pushed an extra '\0' in the resulting string.


Log in to reply
 

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