10 Lines C++, super concise


  • 0
    K
        string convert(string s, int numRows) {
            if (numRows <= 1)   return s;
            vector<string> memo(numRows, "");
            for (int i = 0, row = 0, step = -1; i < s.size(); ++i, row += step) {
                memo[row] += s[i];
                if (row == numRows - 1 || row == 0) step *= -1;
            }
            string res = "";
            for (auto &ss : memo)   res += ss;
            return res;
        }
    

Log in to reply
 

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