-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path006.ZigZagConversion.cpp
More file actions
83 lines (73 loc) · 2.47 KB
/
006.ZigZagConversion.cpp
File metadata and controls
83 lines (73 loc) · 2.47 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
/*
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/
/*method:
//n=numRows
Δ=2n-2 1 2n-1 4n-3
Δ= 2 2n-2 2n 4n-4 4n-2
Δ= 3 2n-3 2n+1 4n-5 .
Δ= . . . . .
Δ= . n+2 . 3n .
Δ= n-1 n+1 3n-3 3n-1 5n-5
Δ=2n-2 n 3n-2 5n-4
*/
//===================code1================
class Solution {
public:
std::string convert(std::string s, int numRows) {
if(s.size() <= 1 || numRows < 2) return s;
std::vector<std::string> sVec(numRows);
bool down = true;
for( int i = 0, j = 0, iEnd = s.size(); i < iEnd; ++i) {
sVec[j].push_back(s[i]);
j = down? j+1:j-1;
if(j == numRows) {
j = numRows - 2;
down = false;
}
else if(j < 0) {
j = 1;
down = true;
}
}
std::string result;
for(auto&& value : sVec) {
result.append(value);
}
return result;
}
};
//==========================Code2==================
//Code 2:
std::string convert(std::string s, int nRows)
{
if (s.size() <= 1 || nRows <= 1) return s;
std::string result;
//第一行元素
for (int i = 0, iEnd = s.size(); i < iEnd; i += nRows*2-2)
result.append(1, s[i]);
//中间元素
for (int r = 1; r < nRows-1; ++r)
{
const int steps = 2 * nRows - 2 * r - 2;
for (int i = r, iEnd = s.size(), k = 0; i < iEnd;)
{
result.append(1, s[i]);
if (k++ % 2 == 0)
i += steps;
else
i += r*2;
}
}
//最后一行元素
for (int i = nRows - 1, iEnd = s.size(); i < iEnd; i += nRows * 2 - 2)
result.append(1, s[i]);
return result;
}