-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigzag_conversion.cpp
More file actions
85 lines (81 loc) · 2.71 KB
/
Copy pathzigzag_conversion.cpp
File metadata and controls
85 lines (81 loc) · 2.71 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
string convert(string s, int numRows) {
if(s.empty())
{
return s;
}
if(1 == numRows)
{
return s;
}
int s_size = s.size();
char* buff = new char[s_size+1];
buff[s_size+1] = '\0';
int buff_pos = 0;
int param1 = 2 * numRows - 2;
const char* data = s.data();
int temp_index = 0;
int temp_index2 = 0;
//int start_number = s_size / numRows;
for(auto i = 0; i < numRows; i++)
{
if(i == 0 || i == numRows-1)
{
for(auto j = 0;;j++)
{
temp_index = i + j * param1;
if(temp_index >= s_size)
{
break;
}
buff[buff_pos++] = data[temp_index];
//cout << "1 temp index:" << temp_index << " buff pos:" << buff_pos - 1 << " buff value:" << buff[buff_pos-1] << endl;
}
}
else
{
for(auto j = 0;;j++)
{
if(0 == (j % 2))
{
temp_index = i + (j / 2) * param1;
if(temp_index >= s_size)
{
break;
}
buff[buff_pos++] = data[temp_index];
//cout << "2 temp index:" << temp_index << " buff pos:" << buff_pos - 1 << " buff value:" << buff[buff_pos-1] << endl;
}
else
{
temp_index2 = param1 * (j / 2) + 2 * numRows - i - 2;
if(temp_index2 >= s_size)
{
break;
}
buff[buff_pos++] = data[temp_index2];
//cout << "3 temp index:" << temp_index2 << " buff pos:" << buff_pos - 1 << " buff value:" << buff[buff_pos-1] << endl;
}
}
}
}
return buff;
}
};
const static string test1 = {"PAYPALISHIRING"};
const static string test2 = {"PAYP"};
const static string test3 = {"wlrbbmqbhcdarzowkkyhiddqscdxrjmowfrxsjybldbefsarcbynecdyggxxpklorellnmpapqfwkhopkmco"};
int main(void)
{
Solution solution;
cout << solution.convert(test1,4) << endl;
cout << "******************************" << endl;
cout << solution.convert(test2,3) << endl;
cout << "******************************" << endl;
cout << solution.convert(test3,61) << endl;
return 0;
}