-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6. ZigZag Conversion.cpp
49 lines (47 loc) · 1.09 KB
/
6. ZigZag Conversion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Solution
{
public:
void firstRow(int index, int r, string &res, string &s)
{
if (index >= s.length())
{
return;
}
res += s[index];
firstRow(index + r + (r - 2), r, res, s);
}
void middleRows(int index, int r, int prev, string &res, string &s)
{
if (index >= s.length())
{
if (index - prev < s.length())
{
res += s[index - prev];
}
return;
}
if (index <= r)
{
res += s[index];
}
else
{
res += s[index - prev];
res += s[index];
}
middleRows(index + r + (r - 2), r, prev, res, s);
}
string convert(string s, int numRows)
{
if (numRows == 1)
return s;
string res = "";
firstRow(0, numRows, res, s);
for (int i = 0; i < numRows - 2; i++)
{
middleRows(0 + i + 1, numRows, 2 * (i + 1), res, s);
}
firstRow(0 + numRows - 1, numRows, res, s);
return res;
}
};