-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZigZagConversion.cpp
59 lines (48 loc) · 1.29 KB
/
ZigZagConversion.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
58
59
#include <algorithm>
#include <assert.h>
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
string convert(string s, int numRows) {
int r = 0;
int c = 0;
int i = 0;
vector<vector <char> > v(numRows); //no need for allocating second dim
while (i < s.length()){
while (r < numRows && i < s.length())
{
v[r].push_back(s[i]);
cout<<r<<","<<s[i]<<" : ";
i++;r++;
}
--r; --r;
while (r >= 0 && i < s.length())
{
v[r].push_back(s[i]);
cout<<r<<","<<s[i]<<" : ";
i++; --r;
}
cout<<endl<<r<<endl;
++r;++r;
}
string outStr;
for (int row = 0; row < v.size(); ++row){
auto iter = v[row].begin();
while (iter != v[row].end()) {
outStr += *iter;
iter++;
}
}
return outStr;
}
};
int main() {
Solution s;
auto outStr = s.convert("PAYPALISHIRING",4);
//assert(outStr == "PAHNAPLSIIGYIR");
cout<<outStr<<endl;
return 0;
}