-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q6.cpp
66 lines (55 loc) · 1.24 KB
/
Q6.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
63
64
65
66
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
// 运用题目的周期性
string convert_1(string s, int numRows) {
if (numRows == 1){
return s;
}
int str_len = s.size();
vector<string > str_matrix;
for (int i = 0; i < numRows; i++){
str_matrix.push_back("");
}
for (int i = 0; i < str_len; i++){
int row_idx = i % (2 * numRows - 2);
if (row_idx >= numRows){
row_idx = 2 * numRows - 2 - row_idx;
}
str_matrix[row_idx] += s[i];
}
string result = "";
for (int i = 0; i < numRows; i++){
result += str_matrix[i];
}
return result;
}
// 用爬楼梯的方法
string convert(string s, int numRows){
int str_len = s.size();
if (numRows == 1 || str_len <= numRows) return s;
vector<string> strs(numRows);
int row_idx = 0;
int step = 1;
for (int i = 0; i < str_len; i++){
if (row_idx == numRows - 1) step = -1;
if (row_idx == 0) step = 1;
strs[row_idx] += s[i];
row_idx += step;
}
string result;
for (int i = 0; i < numRows; i++){
result += strs[i];
}
return result;
}
};
int main(void){
string s = "P";
int numRows = 2;
Solution model;
string result = model.convert(s, numRows);
return 0;
}