leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0006.cpp (434B)


      1 class Solution {
      2   public:
      3     string convert(string s, int numRows) {
      4         if (numRows == 1) return s;
      5         vector<string> mat(numRows);
      6 
      7         int mode = 1, x = 0;
      8         for (char c : s) {
      9             mat[x].push_back(c);
     10             x += mode;
     11             if (x == numRows - 1 || x == 0) mode *= -1;
     12         }
     13 
     14         string res = "";
     15         for (const auto &r : mat)
     16             res += r;
     17         return res;
     18     }
     19 };