-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
44 lines (40 loc) · 1001 Bytes
/
solution.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
class Solution
{
public:
string convert(string s, int nRows)
{
int strLength = s.length();
int flag = 0;
string result(s);
if(nRows == 1)
return result;
for(int i = 0;i < strLength ; i += 2*nRows-2)
{
result[flag] = s[i];
flag++;
}
for (int i = 1; i < nRows -1 ; i ++ )
{
int flagM = flag;
int flagN = flag+1;
for(int m = i; m < strLength; m += 2*nRows-2)
{
result[flagM] = s[m];
flagM+=2;
flag++;
}
for(int n = i + 2*(nRows - i - 1);n < strLength;n += 2*nRows-2)
{
result[flagN] = s[n];
flagN+=2;
flag++;
}
}
for(int i = nRows - 1;i < strLength ; i += 2*nRows-2)
{
result[flag] = s[i];
flag++;
}
return result;
}
};