思路1 利用字符串list进行转换
class Solution {
public:
string convert(string s, int numRows) {
if (numRows == 1 || numRows >= s.size()) return s;
vector<string> res(numRows, "");
int step;
int cur_row = 0;
for (int i = 0; s[i]; i++) {
res[cur_row] += s[i];
if (cur_row == numRows - 1) {
step = -1;
} else if (cur_row == 0) {
step = 1;
}
cur_row += step;
}
string res_str = "";
for (int i = 0; i < res.size(); i++) res_str += res[i];
return res_str;
}
};Last updated