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
原文地址:http://www.cnblogs.com/tonix/p/3862751.html