题目思路
问题描述是将字符串按照z字型排列,然后横向输出字符串
设输入的是nrows行,则设置nrows个字符串,第i个字符串存储按z字型排列的第i-1行的字符,然后输出即可
string convert(string s, int nRows) { if (nRows <= 1) return s; const int len = (int)s.length(); string *str = new string[nRows];//设置nrows个字符串 int row = 0, step = 1; for (int i = 0; i < len; ++i) { str[row].push_back(s[i]); if (row == 0) step = 1; else if (row == nRows - 1) step = -1; row += step; } s.clear(); for (int j = 0; j < nRows; ++j) { s.append(str[j]); } delete[] str; return s; }