class Solution { public: string convert(string s, int numRows) { if (numRows == 1){ return s; } string out; for (int i = 0; i < numRows; ++i){ int j = 0; while (true){ int pos = -1; if (j % 2 == 0){ pos = j * (numRows - 1) + i; } else if (i != 0 && i != numRows - 1) { pos = j * (numRows - 1) + numRows - i - 1; } if (pos >= (int)s.length()){ break; } if (pos >= 0){ out.append(1, s[pos]); } j ++; } } return out; } };
时间: 2024-10-12 11:52:17