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

 

相关文章:

  • 2021-07-30
  • 2022-02-22
  • 2021-07-28
  • 2021-12-22
  • 2022-12-23
猜你喜欢
  • 2021-12-16
  • 2021-05-22
  • 2021-11-29
  • 2021-08-18
  • 2021-09-23
  • 2021-06-03
  • 2022-03-04
相关资源
相似解决方案