My 27ms C++ solution


  • 0
    W
    class Solution {
    public:
        string convert(string s, int nRows) {
            string ret = "";
            if(nRows == 1) return string(s);
            int gap = (nRows-1)*2;
            for(int i = 0; i < nRows; i++) {
                //i-th Row;
                int curGap = gap - i*2;
                int curPos = i;
                while(curPos < s.length()) {
                    if(curGap == 0) {
                        curGap = gap-curGap;
                        continue;
                    }
                    ret += s[curPos];
                    curPos += curGap;
                    //Change gap
                    curGap = gap-curGap;
                }
            }
            return ret;
        }
    };

  • 0

    Input: "ABCDEF", 4
    Output: "AFBCED"
    Expected: "ABFCED"

    can you explain this use??? I think it's very strange


  • 0
    W

    nRows is 4.

    A

    B.....F

    C..E

    D

    will be written as ABFCED if you read line by line. I think my program output correct answer for this test case.

Log in to reply
 

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