Elegant C++ seems to work, but gets unexplained Output Limit Exceeded...


  • 0
    C
    string convert(string s, int nRows) {
        if (nRows == 1) return s;
        
        string z;
        
        int step = 0;
        
        for (size_t i = 0; i < nRows; ++i) {
            if (!step)
                step = 2 * nRows - 2;
            
            for (size_t j = i; j < s.size(); j += step)
                z += s[j];
            
            step -= 2;
        }
        
        return z;
    }
    

    On my test, this works well. However, LeetCode is giving me an OLE without explaining what conditions made it fail. Any thoughts?


  • 2
    E

    Er.. I don't think this code would work as the request.

    OLE error means you output more characters than expected.

    Keep one thing in mind, your output string should have the same size with the input string.

    Otherwise, you might get an OLE error.


  • 0
    C

    I've tested it and it seems to work. I just don't know what inputs cause it to fail. Hopefully LeetCode will add contextual details to error messages at some point.


  • 1
    W

    Notice about the case abcdefghijk, the right output is agbfhceikdj, but yours can be agbfjcegikdj, have some extra data.
    I have a code review about your algorithm, and here may be fault.

    for (size_t j = i; j < s.size(); j += step)
        z += s[j];
    step -= 2;
    

    If the input string is long, I beg your output may contains a lot extra data, so OLE.


  • 0
    C

    Thank you for your comments. I thought I was being clever, but overlooked that my zigs weren't zagging! It was a pretty simple fix of bouncing between "step" and "(nRows * 2 -2) - step".


  • 0
    W

    Yeah, you are right. Glad that my case can be helpful for you.


Log in to reply
 

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