-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6.cpp
43 lines (40 loc) · 949 Bytes
/
6.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
class Solution
{
public:
string convert(string s, int numRows)
{
/*
m = 2 * n - 2
i%m
0
1, m-1
2, m-2
...
n-2, n
n-1
*/
if (numRows <= 1 || s.size() <= numRows)
return s;
string res = "";
int m = 2 * numRows - 2; // mod
for (int i = 0; i <= numRows - 1; i++) // row
{
for (int j = 0; j < s.size(); j += m) // period beginning num
{
if (i == 0 || i == numRows - 1)
{
if (i + j < s.size())
res += s[i + j];
}
else
{
if (j + i < s.size())
res += s[j + i];
if (j + m - i < s.size())
res += s[j + m - i];
}
}
}
return res;
}
};