Shortest code, easy to understand


  • 0
    class Solution {
    public:
        string convert(string s, int nRows) {
            if(nRows == 1) return s;
            string* rows = new string[nRows];
            int pi;//determine the row number
            for(int i = 0; i < s.size(); i++){
                pi = i%(2*nRows-2);
                if(pi >= nRows){ pi = 2*nRows-2-pi;}
                rows[pi] += s[i];
            }
            string result;
            for(int i = 0; i < nRows; i++){
                result = result + rows[i];
            }
            return result;
        }
    };

  • 0
    E

    No delete[] rows?


Log in to reply
 

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