Wrong Answer but output and expected are the same!


  • 0
    M

    Wrong Answer

    Input: “ABCDE”, 4

    Output: “ABCED”

    Expected: “ABCED”

    The Output and Expected looks the same but I got wrong answer? I’ve already run the code in Xcode and there is no unprintable characters inside the string.

    Someone could explain it for me?

    string convert(string s, int nRows) {
    
        string res;
        
        if(nRows <= 1 || s.length() <= nRows) 
            return s;
        if(s.length() == 0)
            return res;
        for(int i = 0; i < nRows; i++)
            for(int j = i; ; j = j+2*nRows-2) {
                if(i != 0 && i != nRows-1 && (j-2*i) > 0 && (j-2*2) < s.length()) {
                    res.append(1, s[j-2*i]);
                }
                if(j < s.length())
                    res.append(1, s[j]);
                else
                    break;
            }
    
        return res;
    }

  • 0
    S

    Please check out question 'Why is my output wrong answer even though it looks identical to expected?' in LeetCode FAQ


  • 0
    R

    I have the same problem


Log in to reply
 

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