首页 > 其他 > 详细

LeetCode "ZigZag Conversion"

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

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"

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!