-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path006_ZigZag_Conversion.cpp
47 lines (47 loc) · 1.08 KB
/
006_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
#include<string>
#include<vector>
#include<iostream>
#include<sstream>
using namespace std;
class Solution
{
public:
string convert(string s, int numRows)
{
ostringstream stream;
size_t s_len = s.length();
if( numRows == 1)
return s;
for(size_t i = 0; i < numRows; i++)
{
size_t pos = i;
if(i == 0 or i == numRows-1)
{
while( pos < s_len)
{
stream<<s[pos];
pos += (2*numRows-2);
}
}
else
{
while(pos < s_len)
{
stream<<s[pos];
size_t pos2 = pos +(2*numRows-2*(i+1));
if( pos2 < s_len)
stream<<s[pos2];
pos += (2*numRows-2);
}
}
}
return stream.str();
}
};
int main(int argc, char const *argv[])
{
string s = "PAYPALISHIRING";
Solution sol;
cout<<sol.convert("A", 1);
return 0;
}