-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlt22.cpp
53 lines (52 loc) · 1.22 KB
/
lt22.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
50
51
52
53
class Solution
{
public:
string convert(string s, int numRows)
{
vector<vector<char>> arr(numRows, vector<char>(s.size(), '\0'));
if (numRows == 1)
{
return s;
}
bool gozigzag = false;
int j = 0;
int i = 0;
int push = 0;
int count = 0;
int n = s.size();
while (count < n)
{
for (i; i < numRows && count < n; i++)
{
arr[i][j] = s[count];
count++;
}
i = i - 1;
gozigzag = true;
if (gozigzag && i > 0)
{
while (i != 0)
{
i = i - 1;
j = j + 1;
arr[i][j] = s[count];
count++;
}
gozigzag = false;
i = i + 1;
}
}
string newstring;
for (int x = 0; x < numRows; x++)
{
for (int y = 0; y < arr[x].size(); y++)
{
if (arr[x][y] != '\0')
{
newstring.push_back(arr[x][y]);
}
}
}
return newstring;
}
};