时间轴

2025-11-09

init


题目:

模拟

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
#include <string>
#include <vector>

using std::string;
using std::vector;

class Solution {
public:
string convert(string s, int numRows)
{
if (numRows == 1) {
return s;
}
int i, j, n = s.size();
int offset = numRows + numRows - 2;
int left = n % (offset);
int complete = n - left;
int base;

string res;
vector<vector<char> > grid = vector<vector<char> >(numRows, vector<char>());

for (i = 0; i < complete; i += offset) {
for (j = 0; j < numRows; j++) {
grid[j].push_back(s[i + j]);
}
base = i + j;
for (j = 0; j < numRows - 2; j++) {
grid[numRows - 1 - 1 - j].push_back(s[base + j]);
}
}

for (j = 0; j < left && j < numRows; j++) {
grid[j].push_back(s[j + complete]);
}

if (left > numRows) {
base = j + complete;
for (j = 0; j < left - numRows; j++) {
grid[numRows - 1 - 1 - j].push_back(s[base + j]);
}
}

for (i = 0; i < numRows; i++) {
for (char ch : grid[i]) {
res += ch;
}
}
return res;
}
};
int main()
{
Solution S;
int numRows = 3;
string s = "PAYPALISHIRING";
S.convert(s, numRows);
}