-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6. Zigzag Conversion.cpp
52 lines (51 loc) · 1.08 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
50
51
52
class Solution {
public:
string convert(string s, int numRows)
{
vector<vector<char>> store;
int n = numRows;
if(n==1)
{
return s;
}
for(int i = 0;i<n;i++)
{
vector<char> temp;
store.push_back(temp);
}
bool straight = true;
int j = 0;
for(int i = 0;i<s.size();i++)
{
if(j<n && straight)
{
store[j].push_back(s[i]);
j+=1;
if(j==n)
{
straight = false;
j-=2;
}
}
else
{
store[j].push_back(s[i]);
j-=1;
if(j<0)
{
straight = true;
j+=2;
}
}
}
string toreturn = "";
for(auto x:store)
{
for(auto y:x)
{
toreturn.push_back(y);
}
}
return toreturn;
}
};