class Solution {
public:
string convert(string s, int numRows) {
int n = s.length(), r = numRows;
//一列或者一个元素,直接输出
if (r == 1 || r >= n) {
return s;
}
vector<string> mat(r);
for (int i = 0, x = 0, t = r * 2 - 2; i < n; ++i) {
//首先首个元素加入mat[0],下一个元素加入mat[1],,在下一个加入mat[2],如果不满足i % t < r - 1,下一个元素要加入mat[1]
mat[x] += s[i];
i % t < r - 1 ? ++x : --x;
}
string ans;
//遍历数组
for (auto &row : mat) {
ans += row;
}
return ans;
}
};
07-28
899
05-07
949
04-04