码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Zigzag Conversion

时间:2015-09-14 15:18:51      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

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;
    } 
};

 

[LeetCode] Zigzag Conversion

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4807158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!