intgetRId(int x, int groupSize, int numRows){ x %= groupSize; if (x < numRows) { return x; } else { return (numRows - 1) - (x - numRows + 1); } }
intgetCId(int x, int groupSize, int numRows){ int ans = x / groupSize * (numRows - 1); x %= groupSize; if (x < numRows) { return ans; } else { return ans + x - numRows + 1; } }
public: string convert(string s, int numRows){ if (numRows == 1) return s; int groupSize = numRows * 2 - 2; for (int i = 0; i < s.size(); ++i) { int rId = getRId(i, groupSize, numRows); int cId = getCId(i, groupSize, numRows); v.push_back({s[i], rId, cId}); } sort(v.begin(), v.end()); string ret = ""; for (auto e: v) { ret.push_back(e.c); } return ret; } };