Zigzag Conversion

Posted by Bill on March 11, 2023

Zigzag Conversion

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 s, int numRows);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 Example 1:

 Input: s = "PAYPALISHIRING", numRows = 3
 Output: "PAHNAPLSIIGYIR"
 Example 2:

 Input: s = "PAYPALISHIRING", numRows = 4
 Output: "PINALSIGYAHRPI"
 Explanation:
 P     I    N
 A   L S  I G
 Y A   H R
 P     I
 Example 3:

 Input: s = "A", numRows = 1
 Output: "A"


  Constraints:

  1 <= s.length <= 1000
  s consists of English letters (lower-case and upper-case), ',' and '.'.
  1 <= numRows <= 1000

C++ Solution

没有想到比较好的方法,这题的思路是先找到竖的集合以及斜的集合,然后通过竖的可以获取到最终的头和尾,竖和斜的集合进行处理就可以组成字符串的中间部分了。

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
#include <iostream>
#include <string>
using namespace std;
class Solution {
  public:
    string convert(string s, int numRows) {
      vector<string> zags;
      vector<string> zigs;
      vector<string> middle_vec;
      string header, middle, end;
      int col_index = 0;
      int zig_index = 0;
      if (numRows == 1) {
        return s;
      }
      for (int i = 0; i < s.length(); i += 2 * (numRows - 1)) {
        zags.push_back(s.substr(i, numRows));
      }
      for (int i = numRows; i < s.length(); i += 2 * (numRows - 1)) {
        string tmp = s.substr(i, numRows - 2);
        reverse(tmp.begin(), tmp.end());
        zigs.push_back(tmp);
      }
      while (col_index < zags.size() || zig_index < zigs.size()) {
        if (col_index < zags.size()) {
          header.push_back(zags[col_index][0]);
          if (zags[col_index].size() == numRows) {
            end.push_back(zags[col_index][numRows - 1]);
          }
          if (zags[col_index].size() == numRows) {
            middle_vec.push_back(zags[col_index].substr(1, numRows - 2));
          } else {
            middle_vec.push_back(zags[col_index].substr(1));
          }
          col_index++;
        }
        if (zig_index < zigs.size()) {
          string tmp = zigs[zig_index];
          middle_vec.push_back(zigs[zig_index++]);
        }
      }

      for (int i = 0; i < numRows - 2; i++) {
        int index = 0;
        for (auto str : middle_vec) {
          if (str.size() == numRows - 2) {
            middle.push_back(str[i]);
          } else {
            if (index % 2 == 1){
              if (i > numRows - 2 - str.size() - 1) {
                middle.push_back(str[i - (numRows - 2 - str.size())]);
              }
            } else {
              if (i < str.size()) {
                middle.push_back(str[i]);
              }
            }
          }
          index++;
        }
      }
      return header + middle + end;
    }
};