-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0006.zig-zag-conversion.cpp
62 lines (53 loc) · 1.37 KB
/
0006.zig-zag-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
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
string convert(string s, int numRows)
{
// 这里每一个vector都是一个string,而string也算一维,所以相当于二维vector
vector<string> zigZag(numRows);
int row = -1;
int p = 0;
bool down = true;
while (true) {
if (numRows == 1) {
return s;
}
if (row == 0) {
down = true;
} else if (row == numRows - 1) {
down = false;
}
if (down && p < s.size()) {
zigZag[++row].push_back(s[p++]);
} else if (!down && p < s.size()) {
zigZag[--row].push_back(s[p++]);
} else
break;
}
// 输出时,只要将每个vector存储的string按顺序输出就可以了
string ret;
for (int i = 0; i < numRows; i++) {
ret += zigZag[i];
}
return ret;
}
};
void test(string s, int numRows)
{
Solution so;
string zigZag;
cout << "string: " << s << endl;
zigZag = so.convert(s, numRows);
cout << "zig zag oreder: " << zigZag << endl;
}
int main()
{
string s;
s = "AB";
test(s, 1);
cout << "press enter to continue" << endl;
cin.get();
}