您的位置:首页 > 其它

[LeetCode] Zigzag Conversion

2015-09-14 14:53 423 查看
The key challenge to this problem is to make the code clean. This post has shared a nice example, which is rewritten below in C++.

class Solution {
public:
string convert(string s, int numRows) {
vector<string> vs(numRows, "");
int n = s.length(), i = 0;
while (i < n) {
for (int j = 0; j < numRows && i < n; j++)
vs[j].push_back(s[i++]);
for (int j = numRows - 2; j >= 1 && i < n; j--)
vs[j].push_back(s[i++]);
}
string zigzag;
for (string v : vs) zigzag += v;
return zigzag;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: