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

LeetCode "ZigZag Conversion"

时间:2014-07-23 15:04:36      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   re   

Nothing to hard to think. Just take care of boundary conditions.

class Solution {
public:
    string convert(string s, int nRows) {
        if(s.empty() || nRows == 1) return s;
        vector<string> rec;
        int cnt = nRows;
        while (cnt--)    rec.push_back("");
        int inx = 0, step = 1;
        for (int i = 0; i < s.length(); i++)
        {
            rec[inx] += s[i];
            inx += step;
            if (inx == nRows || inx == -1)
            {                
                step = -step;
                inx += step * 2;
            }
        }
        //
        string r;
        for (int i = 0; i < nRows; i++) r += rec[i];
        return r;
    }
};

LeetCode "ZigZag Conversion",布布扣,bubuko.com

LeetCode "ZigZag Conversion"

标签:style   blog   color   io   for   re   

原文地址:http://www.cnblogs.com/tonix/p/3862751.html

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