-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0006. ZigZag Conversion.cpp
57 lines (48 loc) · 998 Bytes
/
0006. 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
50
51
52
53
54
55
56
57
class Solution
{
public:
string convert(string s, int numRows)
{
if (numRows == 1)
{
return s;
}
vector<vector<char>> m;
for (int i = 0; i < numRows; ++i)
{
vector<char> t;
m.push_back(t);
}
bool d = true;
int i = 0;
for (char c : s)
{
m[i].push_back(c);
if (d)
{
++i;
}
else
{
--i;
}
if (d && i == numRows - 1)
{
d = false;
}
else if (!d && i == 0)
{
d = true;
}
}
string ans = "";
for (auto v : m)
{
for (char c : v)
{
ans += c;
}
}
return ans;
}
};