A concise C++ solution


  • 2
    G
    class Solution {
    public:
        string convert(string s, int numRows) {
            int n = (int)s.size();
            string rows[numRows];
            for (int i = 0; i < n;) {
                for (int j = 0; j < numRows && i < n; ++j, ++i)
                    rows[j].push_back(s[i]);
                for (int j = numRows - 2; j > 0 && i < n; --j, ++i)
                    rows[j].push_back(s[i]);
            }
            return accumulate(rows + 1, rows + numRows, rows[0]);
        }
    };

Log in to reply
 

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