MCPcopy Create free account
hub / github.com/Hsinha11/Leetcode-solutions / convert

Method convert

6.Zigzag-Conversion/zigzag-conversion.cpp:6–20  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4class Solution {
5public:
6 string convert(string s, int numRows) {
7 if (numRows == 1 || (int)s.size() <= numRows) return s;
8 vector<string> rows(min(numRows, (int)s.size()));
9 int curRow = 0;
10 int dir = 1; // 1 for down, -1 for up
11 for (char c : s) {
12 rows[curRow].push_back(c);
13 if (curRow == 0) dir = 1;
14 else if (curRow == numRows - 1) dir = -1;
15 curRow += dir;
16 }
17 string res;
18 for (auto &row : rows) res += row;
19 return res;
20 }
21};
22
23// Helper main for quick local testing

Callers 1

mainFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected