| 4 | class Solution { |
| 5 | public: |
| 6 | string convert(string s, int numRows) { |
| 7 | if (numRows == 1 || (int)s.size() <= numRows) return s; |
| 8 | vector<string> rows(min(numRows, (int)s.size())); |
| 9 | int curRow = 0; |
| 10 | int dir = 1; // 1 for down, -1 for up |
| 11 | for (char c : s) { |
| 12 | rows[curRow].push_back(c); |
| 13 | if (curRow == 0) dir = 1; |
| 14 | else if (curRow == numRows - 1) dir = -1; |
| 15 | curRow += dir; |
| 16 | } |
| 17 | string res; |
| 18 | for (auto &row : rows) res += row; |
| 19 | return res; |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | // Helper main for quick local testing |