题目:
图解思路
实现代码如下:
class Solution {
public:
string convert(string s, int numRows) {
if(numRows < 2) return s;
vector<string> rows;
for(int i = 0;i <numRows;i++)
{
rows.push_back("");
}
int i = 0, flag = -1;
for(auto c : s) {
auto iter = rows.begin();
rows[i] = rows[i]+c;
if(i == 0 || i == numRows -1) flag = - flag;
i += flag;
}
string res = "";
for(string row:rows)
res.append(row);
return res;
}
};
注意:
for(auto c : s) {
auto iter = rows.begin();
rows[i] = rows[i]+c;
if(i == 0 || i == numRows -1) flag = - flag;
i += flag;
}
上面的代码是我们的代码新的字符串相加的核心。