首页 > 其他 > 详细

[LeetCode] Zigzag Conversion

时间:2015-09-14 15:18:11      阅读:208      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!